title: Insert an image into a .docx file
body: <p>I am developing a program that has the feature to dynamically create DocX files. As the title suggests, I am having a problem inserting images into a DocX file. One of the issues, as I see it, is that I am using C# 2.0. (When answering this question, I would like to stress that I do not wish to switch to C# 3.0, so please do not try to persuade me.)<br /><br />
  I have taken a look at the MSDN article at <a href="http://msdn.microsoft.com/en-us/library/office/bb497430.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/office/bb497430.aspx</a>, but when I converted the C# 3.0 code that MSDN uses to C# 2.0, I get a document that does not open, and it gives me the error: "The file Testing.docx cannot be opened because there are problems with the contents," and "No error detail available."</p>
  
  <p>Here is my code:</p>
  
  <pre><code>ImagePart ip_Part;
  WordprocessingDocument wpd_Doc = WordprocessingDocument.Create("C:\\Convert\\Testing.docx", WordprocessingDocumentType.Document);
  public Testing()
  {
      wpd_Doc.AddMainDocumentPart();
      wpd_Doc.MainDocumentPart.Document = new Document();
      wpd_Doc.MainDocumentPart.Document.Body = new Body();
  
      ip_Part = wpd_Doc.MainDocumentPart.AddImagePart(ImagePartType.Png);
      System.IO.FileStream fs_Stream = new System.IO.FileStream("image.png", System.IO.FileMode.Open);
      ip_Part.FeedData(fs_Stream);
  
      AddImageToBody("image.png", wpd_Doc.MainDocumentPart.GetIdOfPart(ip_Part));
  
      AppendText("Here is a test bulleted list:");
  
      wpd_Doc.MainDocumentPart.Document.Save();
  
      //Close the document.
      wpd_Doc.Close();
  }
  
  private void AddImageToBody(string s_ImagePath, string s_RelationshipId)
  {
      //OpenXmlElement oxe_Element = new Numbering();
      Drawing d_Drawing = new Drawing();
      DrawWord.Inline i_Inline = new DrawWord.Inline();
      DrawWord.Extent e_Extent = new DrawWord.Extent();
      e_Extent.Cx = 600075; //63px / 96dpi * 914400
      e_Extent.Cy = 600075; //63px / 96dpi * 914400
      i_Inline.Extent = e_Extent;
  
      DrawWord.DocProperties dp_Prop = new DrawWord.DocProperties();
      //dp_Prop.Id = uint.Parse(System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString());
      dp_Prop.Id = 1;
      dp_Prop.Name = "Picture 1";
      dp_Prop.Description = "An automated image.";
      i_Inline.DocProperties = dp_Prop;
  
      DrawWord.NonVisualGraphicFrameDrawingProperties nvgfdp_Prop = new DrawWord.NonVisualGraphicFrameDrawingProperties();
      Draw.GraphicFrameLocks gfl_Locks = new Draw.GraphicFrameLocks();
      gfl_Locks.NoChangeAspect = true;
      nvgfdp_Prop.GraphicFrameLocks = gfl_Locks;
      i_Inline.NonVisualGraphicFrameDrawingProperties = nvgfdp_Prop;
  
      Draw.Graphic g_Graphic = new Draw.Graphic();
      Draw.GraphicData gd_Data = new DocumentFormat.OpenXml.Drawing.GraphicData();
      gd_Data.Uri = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
      DrawPic.Picture p_Pic = new DrawPic.Picture();
      DrawPic.NonVisualPictureProperties nvpp_Prop = new DrawPic.NonVisualPictureProperties();
      DrawPic.NonVisualDrawingProperties nvdp_Prop = new DrawPic.NonVisualDrawingProperties();
      nvdp_Prop.Id = uint.Parse(System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString());
      //nvdp_Prop.Name = s_ImagePath;
      nvdp_Prop.Name = "New_Image.png";
      nvpp_Prop.NonVisualDrawingProperties = nvdp_Prop;
  
      DrawPic.NonVisualPictureDrawingProperties nvpdp_Prop = new DrawPic.NonVisualPictureDrawingProperties();
      nvpp_Prop.NonVisualPictureDrawingProperties = nvpdp_Prop;
      p_Pic.NonVisualPictureProperties = nvpp_Prop;
  
      DrawPic.BlipFill bf_Fill = new DrawPic.BlipFill();
      Draw.Blip b_Blip = new Draw.Blip();
      Draw.ExtensionList el_List = new Draw.ExtensionList();
      Draw.BlipExtension be_Extension = new Draw.BlipExtension();
      be_Extension.Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}";
      el_List.Append(be_Extension);
      b_Blip.Append(el_List);
      b_Blip.Embed = s_RelationshipId;
      b_Blip.CompressionState = Draw.BlipCompressionValues.Print;
      bf_Fill.Blip = b_Blip;
  
      Draw.Stretch s_Stretch = new Draw.Stretch();
      Draw.FillRectangle fr_Rect = new Draw.FillRectangle();
      s_Stretch.FillRectangle = fr_Rect;
      bf_Fill.Append(s_Stretch);
      p_Pic.BlipFill = bf_Fill;
  
      DrawPic.ShapeProperties sp_Prop = new DrawPic.ShapeProperties();
      Draw.Transform2D t2d_Transform = new Draw.Transform2D();
      Draw.Offset o_Offset = new Draw.Offset();
  
      o_Offset.X = 0;
      o_Offset.Y = 0;
      t2d_Transform.Offset = o_Offset;
  
      Draw.Extents e_Extents = new Draw.Extents();
      e_Extents.Cx = 600075; //63px / 96dpi * 914400
      e_Extents.Cy = 600075; //63px / 96dpi * 914400
      t2d_Transform.Extents = e_Extents;
      sp_Prop.Transform2D = t2d_Transform;
  
      Draw.PresetGeometry pg_Geom = new Draw.PresetGeometry();
      Draw.AdjustValueList avl_List = new Draw.AdjustValueList();
      pg_Geom.AdjustValueList = avl_List;
      pg_Geom.Preset = Draw.ShapeTypeValues.Rectangle;
      sp_Prop.Append(pg_Geom);
      p_Pic.ShapeProperties = sp_Prop;
      gd_Data.Append(p_Pic);
      g_Graphic.GraphicData = gd_Data;
      i_Inline.Graphic = g_Graphic;
      d_Drawing.Inline = i_Inline;
      //oxe_Element.Append(d_Drawing);
      //Run r_Run = new Run(d_Drawing);
      wpd_Doc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(d_Drawing)));
  }
  </code></pre>
  
  <p>(The variable names are bad, but this was just a test program, so I did not spend too much time. Also, I have the lines spaced to mimic the Xml format that MSDN had in its example. If I could use the C# 3.0 syntax, I would, but I am using Visual Studio 2005.)</p>