title: I am merging two Word documents with OpenXML SDK but get a corrupt document when copying an image into a header
body: <p>I have code which works in all sorts of different situations, including when copying images into the body of the document.</p>
  
  <p>The code works when copying (adding) headers and footers from one document to the other, as long as the headers/footers being copied do not contain images.</p>
  
  <p>When I copy a header which has an image in it, then the resulting file is corrupt, and when I try to open it with the OpenXML SDK it throws an exception saying "Compressed part has inconsistent data length".
  I do know that the image has to be created in the HeaderPart (as against the MainDocumentPart when copying into the body).</p>
  
  <p>The code which does the merging of the image looks something like:</p>
  
  <pre><code>    private void AddSourceImagesToDestination(XElement sourceXml, OpenXmlPart sourcePart, OpenXmlPart destPart) {
        foreach(XElement drawingElement in sourceXml.Descendants(_mswDrawingElementName)) {
  
          XAttribute aBlipEmbedAttribute = drawingElement.Descendants(_ablipElementName).First().Attribute(_embedAttributeName);
          string relationshipId = aBlipEmbedAttribute.Value;
          ImagePart sourceImagePart = (ImagePart)sourcePart.GetPartById(relationshipId);
          ImagePart destinationImagePart = ((HeaderPart)destPart).AddImagePart(sourceImagePart.ContentType);
          string newRelationshipId = destPart.GetIdOfPart(destinationImagePart);
          aBlipEmbedAttribute.SetValue(newRelationshipId);
  
          destinationImagePart.FeedData(sourceImagePart.GetStream(FileMode.Open, FileAccess.Read));
        }  
    }
  </code></pre>
  
  <p>The above is called passing the source and destination HeaderParts, and the XML of the source header which will after this be copied into the destination document.  After calling the above procedure, destinationHeaderPart.Header.Save() is called.</p>
  
  <p>As I said above, if there are no images in the source header, then the resulting document is fine (i.e. when the foreach doesn't find any drawing elements in the source XML).</p>
  
  <p>I wonder, though, whether this symptom of the images in the header is perhaps a red herring and the real problem is somewhere else. </p>