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
LVL 2
aquila98Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RaminTechnical AdvisorCommented:
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.
0
aquila98Author 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
0
aquila98Author 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
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

aquila98Author 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
0
aquila98Author 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....
0
aquila98Author 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!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aquila98Author Commented:
now I got it to work!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.