Avatar of aquila98
aquila98
 asked on

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

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
Visual Basic.NETMicrosoft Word

Avatar of undefined
Last Comment
aquila98

8/22/2022 - Mon
Ramin

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.
aquila98

ASKER
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
aquila98

ASKER
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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
aquila98

ASKER
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
aquila98

ASKER
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....
ASKER CERTIFIED SOLUTION
aquila98

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
aquila98

ASKER
now I got it to work!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.