saikat banik

saikat banik

  • NA
  • 36
  • 2.1k

How to reset pictures and the size of the image in word file using C#

Aug 5 2020 9:43 AM
Hi, I am new in C# language.
 
I want to extract images from MS word and reset pictures and the size of each image in MS word and store them into Folder.
 
I have done the extraction of each image from MS word and store it into a folder. but I can't reset the picture and size of that particular image. (For reset the picture and size shortcut key for MS word - (Alt + JP + Q +S))
 
So I thought If I use any MS word Application shortcut Keypress events (Alt + JP + Q +S ) after inlineShape.Select(); line .in SaveInlineShapeToFile function then it' can be work, Can anyone help me to solve this problem. How can I use shortcut Key events activity for MS WORD file in this scenario?
  1. static void Main(string[] args)  
  2. {  
  3. var wordApplication = new Application();  
  4. var document = wordApplication.Documents.Open(@"C:\Users\Desktop\Imagedocextract\Testpic.docx");  
  5. for (var i = 1; i <= wordApplication.ActiveDocument.InlineShapes.Count; i++)  
  6. {  
  7. var inlineShapeId = i;  
  8. var thread = new Thread(() => SaveInlineShapeToFile(inlineShapeId, wordApplication));  
  9. thread.SetApartmentState(ApartmentState.STA);  
  10. thread.Start();  
  11. thread.Join();  
  12. }  
  13. wordApplication.Quit();  
  14. Console.ReadLine();  
  15. }  
  16. protected static void SaveInlineShapeToFile(int inlineShapeId, Application wordApplication)  
  17. {  
  18. var inlineShape = wordApplication.ActiveDocument.InlineShapes[inlineShapeId];  
  19. inlineShape.Select(); // Select each image from MS Word file  
  20. wordApplication.Selection.Copy();  
  21. // Check data is in the clipboard  
  22. if (Clipboard.GetDataObject() != null)  
  23. {  
  24. var data = Clipboard.GetDataObject();  
  25. // Check if the data conforms to a bitmap format  
  26. if (data != null && data.GetDataPresent(DataFormats.Bitmap))  
  27. {  
  28. // Fetch the image and convert it to a Bitmap  
  29. var image = (Image)data.GetData(DataFormats.Bitmap, true);  
  30. var currentBitmap = new Bitmap(image);  
  31. // Save the bitmap to a file  
  32. currentBitmap.Save(@"C:\Users\Desktop\Imagedocextract\SaveImage\" + String.Format("img_{0}.jpg", inlineShapeId));  
  33. }  
  34. }  
  35. }  

Answers (4)