Recently encountered a project is to extract all the picture information from a copy of Word, the function does not seem difficult, as long as the use of Office Microsoft.Office.Interop.Word can solve the problem. There are also many articles on the internet to illustrate how to achieve. But in general, the content of the Web is divided into two, one is implemented using the Clipboard, and one is done by converting the image to a byte array. Individuals tend to the latter, but they encounter problems in the course of practice.
Problem one: the way to achieve image extraction by byte can cause the quality of the extracted images to drop seriously, so that the quality of the picture can not be modified by modifying the image quality code to optimize and improve. So far I have not thought of any way to a good solution, can only be replaced by methods to achieve, but the personal guess is because the picture in Word dpi problem caused, because the original very small image will become very large (size). So it has to be implemented using the Clipboard method, but there are limitations with the first method.
The following separate implementation code is posted
The first, in the way of byte, the key statement is (byte[]) shape. Range.enhmetafilebits;
foreach (InlineShape shape in item. Range.inlineshapes)
{
if (shape. Type = = wdinlineshapetype.wdinlineshapepicture)
{
Get a picture in Word
Byte[] img = (byte[]) shape. Range.enhmetafilebits;
Bitmap bmp = New Bitmap (New MemoryStream (IMG));
}
}
The second way, through the Clipboard, is as follows
foreach (InlineShape shape in item. Range.inlineshapes)
{
Judging type
if (shape. Type = = wdinlineshapetype.wdinlineshapepicture)
{
Saving data with the Clipboard
Shape. Select (); Select the current picture
WordApp.Selection.Copy ();//copy current picture
if (Clipboard.containsimage ())
{
Bitmap bmp = New Bitmap (Clipboard.getimage ());
FileName = System.Guid.NewGuid () + defaultpicextension;
Bmp. Save (Savepath + fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
Problem two: Writing a program through the console may encounter things that the Clipboard is not able to use, and you need to refer to System.Window.Form to solve the problem.
I hope that experienced friends can help explain the problem of the first method, and finally paste the implementation of all the Code
private void Bt_readreport_click (object sender, EventArgs e)
{
Initialize control values
Clearcontrol ();
StringBuilder reportcontent = new StringBuilder ();
Object nothing = System.Reflection.Missing.Value;
Object filename = "file full path and name";
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass ();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Open (ref filename, ref nothing, ref nothing, ref Nothing, ref nothing, ref no, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref Nothi Ng, ref nothing, ref no, ref nothing);
Cycle through the chapters in the article
foreach (Paragraph item in worddoc.paragraphs)
{
if (item! = NULL)
{
if (item. Range.Text.Trim ()! = "")
{
Determine if there is a picture in the range
if (item. Range.InlineShapes.Count! = 0)
{
foreach (InlineShape shape in item. Range.inlineshapes)
{
Judging type
if (shape. Type = = wdinlineshapetype.wdinlineshapepicture)
{
Saving data with the Clipboard
Shape. Select (); Select the current picture
WordApp.Selection.Copy ();//copy current picture
String fileName = "";
if (Clipboard.containsimage ())
{
Bitmap bmp = New Bitmap (Clipboard.getimage ());
FileName = System.Guid.NewGuid () + ". png";
Bmp. Save (Savepath + fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
}
Add the appropriate information to the total directory
Reportcontent.appendline (item. Range.Text.Trim ());
}
}
}
Worddoc.close (ref nothing, ref no, ref nothing);
Wordapp.quit (ref nothing, ref no, ref nothing);
}
Yes, the PNG effect is better than JPG, and the file is not big, recommended to use
C # extracting picture problems in Word files