title: How do I copy an Image from one Doc to another with OpenXML and C#?
body: <p>I (OpenXML Newbie) need to extend a program which expands own templates.  There is a table with person name and a description. The description should be taken from another document. This part works fine with unformatted text.</p>
  
  <p>Now the customer wants to use formatted text and pictures in the description part. 
  The formatting works, but I couldn't get the image part to work.  Word 2007 couldn't open the document any more.</p>
  
  <p>I assume that something with the relationship went wrong. I found two parts in the XML which smell:</p>
  
  <ul>
  <li>In the original document the image was in the path: <code>/word/media/image1.bin</code> now in the generated doc the image is in <code>/media/image.bin</code>. Can this cause a problem?  In  <code>/word/_rels/document.xml</code> the path points to correct ressource: <code>Target="/media/image.bin"</code> </li>
  <li>Word seems to loose the "pic" namespace: In the generated doc I have the tags: <code>&lt;a:pic&gt;, &lt;a:nvPicPr&gt;, ...</code> instead of <code>&lt;pic:pic&gt;, &lt;pic:ncvPicPr&gt;, ...</code> </li>
  </ul>
  
  <p>Can you please help me, with this issue or point me to a tutorial ressource? Because large parts of the document already exist I am constricted in the way to implement this (e.g. I have to return a TableCell).  </p>
  
  <h2>Edit:</h2>
  
  <p>I just edited the generated document.xml by hand: I replaced the <code>&lt;a:...&gt;</code> with <code>&lt;pic:...&gt;</code> and now it seems to work. So the problem is how to generate the <code>&lt;pic:...&gt;</code> elements correctly.  </p>
  
  <p>Here are some code snippets regarding the relationship:</p>
  
  <pre><code>...
  ImagePart myIP = mainPart.AddImagePart(iP.ContentType);
  // mIPRelId is a string which ist later used (blip)
  mIPRelId = mainPart.CreateRelationshipToPart(myIP); 
  
  // iP is the imagepart from the source doc...
  myIP.FeedData(iP.GetStream());                  
  // myDR is the local drawing
  // generate Inline should generate the inline part 
  myDR.Inline = generateInline(myXMLDrawing, nsManager);
  ...
  // later when generating the blip
  if (myChild.Name == "a:blip")
  {
      DocumentFormat.OpenXml.Drawing.Blip myBlip = new Blip();
      myBlip.Embed = mIPRelId;
      ...
      myBlipFill.Append(myBlip);
   }
  </code></pre>
  
  <p>Here is the snippet that generates the picture part (namespace prob):</p>
  
  <pre><code> ...
   DocumentFormat.OpenXml.Drawing.Picture myPic = 
      new DocumentFormat.OpenXml.Drawing.Picture();
   string myNodes = ".//descendant::pic:nvPicPr | ";
          myNodes += ".//descendant::pic:blipFill | ";
          myNodes += ".//descendant::pic:spPr | ";
          myNodes += ".//following::w:bookmarkEnd";
  
   XmlNodeList followingNodesList = myXMLPic.
                 SelectNodes(myNodes, nsManager);
  
   myPic.AddNamespaceDeclaration(
                     "pic", 
                     "http://schemas.openxmlformats.org/drawingml/2006/picture");
  
  
    // stepping through the descendants and adding them
    // to the picture
    foreach (XmlElement el in followingNodesList)
    {
        if (el.Name == "pic:nvPicPr")
        {
           DocumentFormat.OpenXml.Drawing.NonVisualPictureProperties myNVPP = 
                                                    new NonVisualPictureProperties();
           ...
           myPic.Append(myNVPP);
        }
        ...          
        // Adding the other parts like blipfill....
  </code></pre>
  
  <p>Here is a snippet of the generated XML (sorry for the formatting):</p>
  
  <pre><code> ...
   &lt;a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"&gt;
     &lt;a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"&gt;
       &lt;a:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"&gt;
         &lt;a:nvPicPr&gt;
           &lt;a:cNvPr id="0" name="MS_HUND.JPG" /&gt; 
           &lt;a:cNvPicPr /&gt; 
         &lt;/a:nvPicPr&gt;
         &lt;a:blipFill&gt;
           &lt;a:blip r:embed="R65532bbe7935423e" cstate="print" /&gt; 
           &lt;a:stretch&gt;
             &lt;a:fillRect /&gt; 
           &lt;/a:stretch&gt;
         &lt;/a:blipFill&gt;
         &lt;a:spPr&gt;
           &lt;a:xfrm&gt;
             &lt;a:off x="0" y="0" /&gt; 
             &lt;a:ext cx="714375" cy="762000" /&gt; 
           &lt;/a:xfrm&gt;
           &lt;a:prstGeom prst="rect"&gt;
             &lt;a:avLst /&gt; 
           &lt;/a:prstGeom&gt;
         &lt;/a:spPr&gt;
       &lt;/a:pic&gt;
     &lt;/a:graphicData&gt;
   &lt;/a:graphic&gt;
   ...
  </code></pre>