wordpress hit counter
Re: How to set a System.Drawing.Imaging.Metafile into a ImagePart ? - WordprocessingML - Formats - OpenXML Developer

Re: How to set a System.Drawing.Imaging.Metafile into a ImagePart ?

Formats

Discussions about working with different Open XML Formats

How to set a System.Drawing.Imaging.Metafile into a ImagePart ?

  • rated by 0 users
  • This post has 3 Replies |
  • 1 Follower
  • Hello,

    How to set a System.Drawing.Imaging.Metafile into a ImagePart ?

    Example code:

    private ImagePart AddImagePart (MainDocumentPart mainPart, Metafile image)
            {
                ImagePart l_ImagePart = mainPart.AddImagePart(ImagePartType.Emf);
                image.Save(l_ImagePart.GetStream(FileMode.Create), ImageFormat.Emf); // Exception
                return l_ImagePart;
            }

    Some ideas ?

    Thanks

    Tonite
  • I have found a way....

            [DllImport("gdi32", EntryPoint = "CopyEnhMetaFile")]
            public static extern int CopyEnhMetaFileA(int hemfSrc, string lpszFile);

            [DllImport("gdi32")]
            public static extern int DeleteEnhMetaFile(int hemf);

            private ImagePart AddImagePart(MainDocumentPart mainPart, Metafile image)
            {
                string l_TempFileName = Path.GetTempFileName();

                IntPtr l_H1 = image.GetHenhmetafile();

                int l_H2 = CopyEnhMetaFileA(l_H1.ToInt32(), l_TempFileName);

                DeleteEnhMetaFile(l_H2);

                ImagePart l_ImagePart = mainPart.AddImagePart(ImagePartType.Emf);

                using (FileStream l_Stream = new FileStream(l_TempFileName, FileMode.Open))
                {
                    l_ImagePart.FeedData(l_Stream);
                }

                DeleteEnhMetaFile(l_H1.ToInt32());

                File.Delete(l_TempFileName);

                return l_ImagePart;
            }


    Better suggestions welcome....

    Regards

    Tonite
  • Are you getting an error with the encoder ?
    "Value cannot be null. Parameter name: encoder"

    If so, it just because of the .net framework.
    http://msdn.microsoft.com/en-us/library/ktx83wah.aspx

    "If no encoder exists for the file format of the image, the Portable Network Graphics (PNG) encoder is used. When you use the Save method to save a graphic image as a Windows Metafile Format (WMF) or Enhanced Metafile Format (EMF) file, the resulting file is saved as a Portable Network Graphics (PNG) file. This behavior occurs because the GDI+ component of the .NET Framework does not have an encoder that you can use to save files as .wmf or .emf files.

    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

Page 1 of 1 (4 items)