Image load from docx

waraq

Member
Joined
Dec 18, 2006
Messages
23
Location
Harrison, TN
Programming Experience
Beginner
I have several images that I need to load in a picture box. The extention is .docx. How can I load these images?
I tried this:
VB.NET:
Archivo.Filter = "All Images|*.BMP;*.GIG;*.GIF;*.JPEG;*.JPG;*.TIF;*.EXIF;*.EMF;*.ICO;*.PNG;*.WMF;*.DOCX"
        If Archivo.ShowDialog = DialogResult.OK Then
            Imagen = Image.FromFile(Archivo.FileName)
            Pic.Image = Imagen
        End If

but it didn't work. I have a message saiying that I'm out of memory and I need to assign the memory size. I don't know how to assign the size.

I also tried this:

VB.NET:
Dim Image as image
Imagen = image.fromfile("PicW.docx")
Pic.Image = Imagen
But this set of codes didn't worked either.
So what am I doing wrong here?

I would appreciate your help.

Willy
 
Last edited by a moderator:
Thank you for the link. I know is not an image file, becouse I create the image in word document. Then the question is how I open the file wiht extention *.docx (which is word document with an image in it). I know you just said that you don't know and I appreciate your help.

Willy
 
One easy option is to open the Word document and copy the image, paste it into an image editor application and save the image file. Now you can access the Image.FromFile.

Another easy option is to unzip the docx file and find the image file in Word/Media folder. You can also use a zip library to do this programmatically (SharpZipLib)

For Office automation library I doubt the older libraries will work, Office 2007 uses new file formats (zip compressed file structure, xml based content). This should be more relevant: Open XML File Formats: What is it, and how can I get started?

To use System.IO.Packaging namespace classes you need .Net 3.0 and VS2005 Workflow extensions, then reference WindowsBase (.Net tab in References dialog) and Imports System.IO.Packaging. Here is a code sample:
VB.NET:
'Imports System.IO.Packaging

Dim path As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Doc1.docx")
Using doc As Package = Package.Open(path)
    Dim imgUri As New Uri("/word/media/image1.png", UriKind.Relative)
    PictureBox1.Image = Image.FromStream(doc.GetPart(imgUri).GetStream)
End Using
to have a look at the package parts Uris try this:
VB.NET:
For Each part As PackagePart In doc.GetParts()
    MsgBox(part.Uri.ToString)
Next
 
Back
Top