0

Can I read images and shapes from a Word file and display them in picture box control in C# pragmatically?

Actually I have a Word file in which I have questions with multiple answer and some questions also contain images and shapes.

So what I want to do is to read the document in C# and show the images and shapes to display in pictureBoxcontrol. So is it possible in C# and if it is then what is the solution. I have searched a lot on Google but didn't found.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
jahanzaib kk
  • 688
  • 2
  • 10
  • 26

3 Answers3

2

I haven't done Word automation for a while and I never used this function, but you can try using Range.Copy - you'll have to locate the various Shape objects and images, copy them and they try pasting them into your picture box.

Also look at this question and this one.

Community
  • 1
  • 1
xxbbcc
  • 16,930
  • 5
  • 50
  • 83
1

You can create either a macro in your word document or in your c# code. I don't know how to "simulate" a vba macro from your c# code but I know that you'll find lots of solution on stackoverflow. The code you need is something like.

activeDocument.Shapes(x).select 

or

activeDocument.InlineShapes(x).select 

you have to check if your image is an inlineshape or shape. Then copy it with

Selection.copy

That's the vba code. the image is now in your clipboard so you need following c# code to retrieve it.

if (Clipboard.ContainsImage())
{
   Image img = Clipboard.GetImage();
}
lorenz albert
  • 1,385
  • 2
  • 11
  • 27
1

You can loop through your Word document like the code above. What it basically does is, it takes every picture, graph, table.

foreach (NetOffice.WordApi.InlineShape s in docWord.InlineShapes)
{
      #region Set Shapes
      if (s.Type==NetOffice.WordApi.Enums.WdInlineShapeType.wdInlineShapePicture &&  s.AlternativeText.Contains("|"))
      {
             Clipboard.SetImage(s.Select());
      }
}

Now to set it into your PictureBoxControl:

pictureBox1.Image = new Bitmap(@"\Program Files\PictureBoxControl\tinyemulator_content.jpg");

And from Clipboard:

pictureBox1.Image = new Bitmap(Clipboard.GetImage());
mike27015
  • 686
  • 1
  • 6
  • 19
  • visual studio is now giving the error "Argument type void is not assignable to parameter type 'System.Drawing.Image'" on `Clipboard.SetImage(s.Select());` so how i resolved this error. plz help me as i have accepted your answer but now facing problem\ – jahanzaib kk Jun 17 '13 at 11:09