Saving the image to the same file it was constructed from is not allowed and throws an exception."
Before saving just test if the image format is EMF and use the PNG format instead.
if (image.RawFormat.Guid == ImageFormat.Emf.Guid) image.Save(imagePart.GetStream(), ImageFormat.Png);else image.Save(imagePart.GetStream(), image.RawFormat);
Hope that helps
@Tonight: Great code, it was the only thing I found to help me with the same situation. I've ported to VB.NET in case someone else needs this in the future.
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Presentation
Imports DocumentFormat.OpenXml
Imports System.IO
Imports System.Drawing.Imaging
Module Module1
Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As Integer, ByVal lpszFile As String) As Integer
Declare Function DeleteEnhMetaFile Lib "gdi32" (hemf As Integer) As Integer
Private Function AddImagePart(mainPart As MainDocumentPart, image As Metafile) As ImagePart
Dim l_TempFileName As String = Path.GetTempFileName()
Dim l_H1 As IntPtr = image.GetHenhmetafile()
Dim l_H2 As Integer = CopyEnhMetaFile(l_H1.ToInt32(), l_TempFileName)
DeleteEnhMetaFile(l_H2)
Dim l_ImagePart As ImagePart = mainPart.AddImagePart(ImagePartType.Emf)
Using l_Stream As New FileStream(l_TempFileName, FileMode.Open)
l_ImagePart.FeedData(l_Stream)
End Using
DeleteEnhMetaFile(l_H1.ToInt32())
File.Delete(l_TempFileName)
Return l_ImagePart
End Function
End Module