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
Avatar of Ramin
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.
Avatar of aquila98
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
Avatar of aquila98
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
Avatar of aquila98
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
Avatar of aquila98
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
Avatar of aquila98
aquila98

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of aquila98
aquila98

ASKER

now I got it to work!
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo