how to extract images from word document with same quality and size ?

aquila98
aquila98 used Ask the Experts™
on
Hello

I am trying to export all the images in a generated word document...
I wound some code on the net such as this one:
For Each i As Word.InlineShape In mWordDocument.InlineShapes
            i.Select()
            mWordApplication.Selection.CopyAsPicture()
            Dim bmp As Image = Clipboard.GetImage()
            If bmp IsNot Nothing Then
                If bmp.Size.Width > MINIMUM_BITMAP_WIDTH Then
                    mMapNoImage.Add(iNO, bmp)
                    iNO += 1
                End If
            End If
        Next

This works somewhat... but the quality of the image if not adequate.
Also sometime it only gets part of the image!
Further whereas in the word document I can resize the image and it keeps its clarity, the images exported has poor resolution.

How can I extract all the images and have a jog file with same quality as tat of the object embedded within the word document ???

Thanks for any hints
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
RaminTechnical Advisor

Commented:
Word documents containing embedded images can not be easily extracted. Attempts to copy and paste the images result in poor quality images.

Try these methods,

save the document as a web page using the following steps:
 
1. On the File menu click Save as Web Page
2. In the Save As drop down select Web Page (*.htm; *.html)
 
Images will be extracted from the document and placed in the folder named <DocumentName>_files in the same location as the saved web page.

Author

Commented:
I have been told that there is a way to improve bitmap quality by using windows metafile format and raw bits directly from word.
I'll play with this and if indeed successful, I'll post the solution here (thus answering my own question)

cheers

Author

Commented:
indeed they were right those who suggested metafile!

It works perfectly and the images taken from the word document are as good as when they are in the doc!

Here is the code fI come up with:
Private Sub ExtractImage()
        Dim iNO As Integer = 1
        Dim vData() As Byte
        Dim NewGraphic As Graphics = Nothing

        For Each i As Word.InlineShape In mWordDocument.InlineShapes
            vData = i.Range.EnhMetaFileBits
            Using mem As New IO.MemoryStream(vData)
                Using mf As New Imaging.Metafile(mem)
                    If mf.Width > MINIMUM_BITMAP_WIDTH Then
                        Using bmp As New Bitmap(mf.Width, mf.Height)
                            NewGraphic = Graphics.FromImage(bmp)
                            NewGraphic.FillRectangle(New SolidBrush(Color.White), 0, 0, mf.Width, mf.Height)
                            NewGraphic.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                            NewGraphic.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                            NewGraphic.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
                            NewGraphic.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
                            NewGraphic.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit

                            NewGraphic.DrawImage(mf, 0, 0, mf.Width, mf.Height)
#If DEBUG Then
                            bmp.Save(String.Format("d:\temp\x1_{0}.jpg", iNO.ToString("000")), Imaging.ImageFormat.Jpeg)
#End If
                            mMapNoImage.Add(iNO, bmp)
                            iNO += 1

                            NewGraphic.Dispose()
                        End Using
                    End If
                End Using
            End Using
        Next
    End Sub
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
strangely, with this method I get sometimes just hals of the picture!
The quality is excellent but sometimes part of the picture is exported in the metafile...

Why ????? I have yet to figure out!

Any one here have any idea why this method does not get all the image ???


thanks for any hints

cheers

Author

Commented:
very weird!,,,
the document  is landscape  legal and some of the biases are cut while others are perfect!

still working  on  figuring  out  what  is going  on  with  this Metafile  extraction... any help welcomed....
Commented:
I think I got it...
Seems like this is due to a difference between DPI in displayed image and that stored in the document (100 vs 300 DPI). By scaling on the x axis by a factor of 30% it works...
                                    NewGraphic.DrawImage(mf, New Drawing.Rectangle(0, 0, mf.Width * ENLARGING_FACTOR, mf.Height), 0, 0, mf.Width, mf.Height, GraphicsUnit.Document)


I guess this closes the question!

Author

Commented:
now I got it to work!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial