使用.NET 将图像转换为WMF?

发布于 2024-10-21 13:38:15 字数 517 浏览 7 评论 0原文

有很多将 wmf 转换为位图的示例,例如: 可靠的 .wmf/wmf 到基于像素的图像转换

但是我需要反向操作。我不寻找矢量化器。我只想将图片嵌入到 wmf 文件中,而不必担心 wmf 格式的位和字节。我需要一个 .NET 解决方案,最好是 C# 。

我首先认为这可以完成这项工作:

using (Image img = Image.FromFile (path)) {
    img.Save (myStream, System.Drawing.Imaging.ImageFormat.Wmf);
}

但这在运行时抱怨编码器为空。我在哪里/如何构建这样的编码器?我不需要一个复杂的,只需要一个将图像包装成 wmf 的。 WMF 支持的格式有什么要求吗?我想支持 png 和 bmp,但也支持 gif 吗?

There are plenty of examples of converting a wmf into a bitmap like:
Reliable .wmf/wmf to Pixel based image conversion

But I need the reverse operation. I do not look for a vectorizer. I just want to embed a picture inside a wmf file without having to bother about the bits and bytes of the wmf format. I need a solution for .NET preferably in C#.

I first thought this would do the job:

using (Image img = Image.FromFile (path)) {
    img.Save (myStream, System.Drawing.Imaging.ImageFormat.Wmf);
}

But this complains at runtime that the encoder is null. Where/How can I build such an encoder? I do not need a complicated one, just one that wraps an image into a wmf. Are there some requirements on the supported formats in WMF? I suppose png and bmp are supported but is gif also supported?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆离笙 2024-10-28 13:38:15

来自此处

当您使用 Save 方法保存
作为 Windows 图元文件的图形图像
格式 (WMF) 或增强型图元文件
格式化(EMF)文件,生成的文件
另存为便携式网络
而是图形 (PNG) 文件。这
发生这种情况的原因是 GDI+
.NET Framework 的组件确实
没有可以使用的编码器
将文件另存为 .wmf 或 .emf 文件。

但我想你已经做到了:)

这里有人正在将位图放入文件流中。

metafileStream = MakeMetafileStream(gdiBitmap);

MakeMetafileStream() 是:

private static MemoryStream MakeMetafileStream(Bitmap image)
{
  Graphics graphics = null;
  Metafile metafile= null;
  var stream = new MemoryStream();
  try
  {
    using (graphics = Graphics.FromImage(image))
    {
      var hdc = graphics.GetHdc();
      metafile= new Metafile(stream, hdc);
      graphics.ReleaseHdc(hdc);
    }
    using (graphics = Graphics.FromImage(metafile))
    { graphics.DrawImage(image, 0, 0); }
  }
  finally
  {
    if (graphics != null)
    { graphics.Dispose(); }
    if (metafile!= null)
    { metafile.Dispose(); }
  }
  return stream;
}

有趣的东西。
但至于编码器的事情...

此处来自 MS 的 Peter Huang 发布了这种非托管方法:

        [DllImport("gdiplus.dll")]
        private static extern uint GdipEmfToWmfBits (IntPtr _hEmf, uint _bufferSize,
            byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);
        [DllImport("gdi32.dll")]
        private static extern IntPtr SetMetaFileBitsEx (uint _bufferSize,
            byte[] _buffer);
        [DllImport("gdi32.dll")]
        private static extern IntPtr CopyMetaFile (IntPtr hWmf,
            string filename);
        [DllImport("gdi32.dll")]
        private static extern bool DeleteMetaFile (IntPtr hWmf);
        [DllImport("gdi32.dll")]
        private static extern bool DeleteEnhMetaFile (IntPtr hEmf);
        private void button4_Click(object sender, System.EventArgs e)
        {
            Graphics g= this.CreateGraphics();
            IntPtr hDC = g.GetHdc();
            Metafile mf = new Metafile(hDC,EmfType.EmfOnly);
            g.ReleaseHdc(hDC);
            g.Dispose();
            g=Graphics.FromImage(mf);
            //Pen p = new Pen(Color.White,5);
            g.DrawArc(Pens.Black,0,0,200,200,0,360);
            //g.DrawImage(Bitmap.FromFile(@"c:\temp\test.bmp"),0,0);
            g.Dispose();
            IntPtr _hEmf= mf.GetHenhmetafile();
            uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
                EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
            byte[] _buffer = new byte[_bufferSize];
            GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC,
                    EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
            IntPtr hmf = SetMetaFileBitsEx(_bufferSize, _buffer);
            CopyMetaFile(hmf, "C:\\ConvertedMetafile.wmf");
            DeleteMetaFile(hmf);
            DeleteEnhMetaFile(_hEmf);
        }

希望这能让你到达那里:)

From here:

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 instead. 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.

But I guess you already got that far :)

Here someone is putting a bitmap in a FileStream.

metafileStream = MakeMetafileStream(gdiBitmap);

with MakeMetafileStream() being:

private static MemoryStream MakeMetafileStream(Bitmap image)
{
  Graphics graphics = null;
  Metafile metafile= null;
  var stream = new MemoryStream();
  try
  {
    using (graphics = Graphics.FromImage(image))
    {
      var hdc = graphics.GetHdc();
      metafile= new Metafile(stream, hdc);
      graphics.ReleaseHdc(hdc);
    }
    using (graphics = Graphics.FromImage(metafile))
    { graphics.DrawImage(image, 0, 0); }
  }
  finally
  {
    if (graphics != null)
    { graphics.Dispose(); }
    if (metafile!= null)
    { metafile.Dispose(); }
  }
  return stream;
}

Interesting stuff.
But as to the encoder thing...

Here Peter Huang from MS posted this unmanaged approach:

        [DllImport("gdiplus.dll")]
        private static extern uint GdipEmfToWmfBits (IntPtr _hEmf, uint _bufferSize,
            byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);
        [DllImport("gdi32.dll")]
        private static extern IntPtr SetMetaFileBitsEx (uint _bufferSize,
            byte[] _buffer);
        [DllImport("gdi32.dll")]
        private static extern IntPtr CopyMetaFile (IntPtr hWmf,
            string filename);
        [DllImport("gdi32.dll")]
        private static extern bool DeleteMetaFile (IntPtr hWmf);
        [DllImport("gdi32.dll")]
        private static extern bool DeleteEnhMetaFile (IntPtr hEmf);
        private void button4_Click(object sender, System.EventArgs e)
        {
            Graphics g= this.CreateGraphics();
            IntPtr hDC = g.GetHdc();
            Metafile mf = new Metafile(hDC,EmfType.EmfOnly);
            g.ReleaseHdc(hDC);
            g.Dispose();
            g=Graphics.FromImage(mf);
            //Pen p = new Pen(Color.White,5);
            g.DrawArc(Pens.Black,0,0,200,200,0,360);
            //g.DrawImage(Bitmap.FromFile(@"c:\temp\test.bmp"),0,0);
            g.Dispose();
            IntPtr _hEmf= mf.GetHenhmetafile();
            uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
                EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
            byte[] _buffer = new byte[_bufferSize];
            GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC,
                    EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
            IntPtr hmf = SetMetaFileBitsEx(_bufferSize, _buffer);
            CopyMetaFile(hmf, "C:\\ConvertedMetafile.wmf");
            DeleteMetaFile(hmf);
            DeleteEnhMetaFile(_hEmf);
        }

Hope this'll get you there :)

夜雨飘雪 2024-10-28 13:38:15

这是问题的完整答案,包括我的修改。文森特的回答是完全正确的。仅缺少一些定义和一个枚举。这就是为什么我在这里发布“干净”的工作代码,希望它对其他人有用。

        [Flags]
        private enum EmfToWmfBitsFlags {
            EmfToWmfBitsFlagsDefault = 0x00000000,
            EmfToWmfBitsFlagsEmbedEmf = 0x00000001,
            EmfToWmfBitsFlagsIncludePlaceable = 0x00000002,
            EmfToWmfBitsFlagsNoXORClip = 0x00000004
        }

        private static int MM_ISOTROPIC = 7;
        private static int MM_ANISOTROPIC = 8;

        [DllImport ("gdiplus.dll")]
        private static extern uint GdipEmfToWmfBits (IntPtr _hEmf, uint _bufferSize,
            byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);
        [DllImport ("gdi32.dll")]
        private static extern IntPtr SetMetaFileBitsEx (uint _bufferSize,
            byte[] _buffer);
        [DllImport ("gdi32.dll")]
        private static extern IntPtr CopyMetaFile (IntPtr hWmf,
            string filename);
        [DllImport ("gdi32.dll")]
        private static extern bool DeleteMetaFile (IntPtr hWmf);
        [DllImport ("gdi32.dll")]
        private static extern bool DeleteEnhMetaFile (IntPtr hEmf);

        private static MemoryStream MakeMetafileStream (Bitmap image)
        {
            Metafile metafile = null;
            using (Graphics g = Graphics.FromImage (image)) {
                IntPtr hDC = g.GetHdc ();
                metafile = new Metafile (hDC, EmfType.EmfOnly);
                g.ReleaseHdc (hDC);
            }

            using (Graphics g = Graphics.FromImage (metafile)) {
                g.DrawImage (image, 0, 0);
            }
            IntPtr _hEmf = metafile.GetHenhmetafile ();
            uint _bufferSize = GdipEmfToWmfBits (_hEmf, 0, null, MM_ANISOTROPIC,
                EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
            byte[] _buffer = new byte[_bufferSize];
            GdipEmfToWmfBits (_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC,
                    EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
            IntPtr hmf = SetMetaFileBitsEx (_bufferSize, _buffer);
            string tempfile = Path.GetTempFileName ();
            CopyMetaFile (hmf, tempfile);
            DeleteMetaFile (hmf);
            DeleteEnhMetaFile (_hEmf);

            var stream = new MemoryStream ();
            byte[] data = File.ReadAllBytes (tempfile);
            //File.Delete (tempfile);
            int count = data.Length;
            stream.Write (data, 0, count);
            return stream;
        }

Here is the full answer to the question including my modifications. Vincent's answer is fully correct. Only some definitions and one enum were missing. That is why I post here the "clean" working code in the hope it can be useful for someone else.

        [Flags]
        private enum EmfToWmfBitsFlags {
            EmfToWmfBitsFlagsDefault = 0x00000000,
            EmfToWmfBitsFlagsEmbedEmf = 0x00000001,
            EmfToWmfBitsFlagsIncludePlaceable = 0x00000002,
            EmfToWmfBitsFlagsNoXORClip = 0x00000004
        }

        private static int MM_ISOTROPIC = 7;
        private static int MM_ANISOTROPIC = 8;

        [DllImport ("gdiplus.dll")]
        private static extern uint GdipEmfToWmfBits (IntPtr _hEmf, uint _bufferSize,
            byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);
        [DllImport ("gdi32.dll")]
        private static extern IntPtr SetMetaFileBitsEx (uint _bufferSize,
            byte[] _buffer);
        [DllImport ("gdi32.dll")]
        private static extern IntPtr CopyMetaFile (IntPtr hWmf,
            string filename);
        [DllImport ("gdi32.dll")]
        private static extern bool DeleteMetaFile (IntPtr hWmf);
        [DllImport ("gdi32.dll")]
        private static extern bool DeleteEnhMetaFile (IntPtr hEmf);

        private static MemoryStream MakeMetafileStream (Bitmap image)
        {
            Metafile metafile = null;
            using (Graphics g = Graphics.FromImage (image)) {
                IntPtr hDC = g.GetHdc ();
                metafile = new Metafile (hDC, EmfType.EmfOnly);
                g.ReleaseHdc (hDC);
            }

            using (Graphics g = Graphics.FromImage (metafile)) {
                g.DrawImage (image, 0, 0);
            }
            IntPtr _hEmf = metafile.GetHenhmetafile ();
            uint _bufferSize = GdipEmfToWmfBits (_hEmf, 0, null, MM_ANISOTROPIC,
                EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
            byte[] _buffer = new byte[_bufferSize];
            GdipEmfToWmfBits (_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC,
                    EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
            IntPtr hmf = SetMetaFileBitsEx (_bufferSize, _buffer);
            string tempfile = Path.GetTempFileName ();
            CopyMetaFile (hmf, tempfile);
            DeleteMetaFile (hmf);
            DeleteEnhMetaFile (_hEmf);

            var stream = new MemoryStream ();
            byte[] data = File.ReadAllBytes (tempfile);
            //File.Delete (tempfile);
            int count = data.Length;
            stream.Write (data, 0, count);
            return stream;
        }
泪是无色的血 2024-10-28 13:38:15

jdehaan 发布的改进版本(顺便说一句,对他和 Vincent 表示赞赏)

    [Flags]
    private enum EmfToWmfBitsFlags
    {
        EmfToWmfBitsFlagsDefault = 0x00000000,
        EmfToWmfBitsFlagsEmbedEmf = 0x00000001,
        EmfToWmfBitsFlagsIncludePlaceable = 0x00000002,
        EmfToWmfBitsFlagsNoXORClip = 0x00000004
    }

    private static int MM_ISOTROPIC = 7;
    private static int MM_ANISOTROPIC = 8;

    [DllImport("gdiplus.dll")]
    private static extern uint GdipEmfToWmfBits(IntPtr _hEmf, uint _bufferSize,
        byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);
    [DllImport("gdi32.dll")]
    private static extern IntPtr SetMetaFileBitsEx(uint _bufferSize,
        byte[] _buffer);
    [DllImport("gdi32.dll")]
    private static extern IntPtr CopyMetaFile(IntPtr hWmf,
        string filename);
    [DllImport("gdi32.dll")]
    private static extern bool DeleteMetaFile(IntPtr hWmf);
    [DllImport("gdi32.dll")]
    private static extern bool DeleteEnhMetaFile(IntPtr hEmf);

    public static MemoryStream MakeMetafileStream(System.Drawing.Bitmap image)
    {
        Metafile metafile = null;
        using (Graphics g = Graphics.FromImage(image))
        {
            IntPtr hDC = g.GetHdc();
            metafile = new Metafile(hDC, EmfType.EmfOnly);
            g.ReleaseHdc(hDC);
        }

        using (Graphics g = Graphics.FromImage(metafile))
        {
            g.DrawImage(image, 0, 0);
        }
        IntPtr _hEmf = metafile.GetHenhmetafile();
        uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
            EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
        byte[] _buffer = new byte[_bufferSize];
        GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC,
                EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
        DeleteEnhMetaFile(_hEmf);

        var stream = new MemoryStream();
        stream.Write(_buffer, 0, (int)_bufferSize);
        stream.Seek(0, 0);

        return stream;
    }

这个版本不会留下临时文件,并且还避免将 _bufferSize 复制到临时文件,然后再将其复制到另一个缓冲区。
再次感谢你们。

An improved version of what jdehaan posted (kudos btw to him and Vincent)

    [Flags]
    private enum EmfToWmfBitsFlags
    {
        EmfToWmfBitsFlagsDefault = 0x00000000,
        EmfToWmfBitsFlagsEmbedEmf = 0x00000001,
        EmfToWmfBitsFlagsIncludePlaceable = 0x00000002,
        EmfToWmfBitsFlagsNoXORClip = 0x00000004
    }

    private static int MM_ISOTROPIC = 7;
    private static int MM_ANISOTROPIC = 8;

    [DllImport("gdiplus.dll")]
    private static extern uint GdipEmfToWmfBits(IntPtr _hEmf, uint _bufferSize,
        byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);
    [DllImport("gdi32.dll")]
    private static extern IntPtr SetMetaFileBitsEx(uint _bufferSize,
        byte[] _buffer);
    [DllImport("gdi32.dll")]
    private static extern IntPtr CopyMetaFile(IntPtr hWmf,
        string filename);
    [DllImport("gdi32.dll")]
    private static extern bool DeleteMetaFile(IntPtr hWmf);
    [DllImport("gdi32.dll")]
    private static extern bool DeleteEnhMetaFile(IntPtr hEmf);

    public static MemoryStream MakeMetafileStream(System.Drawing.Bitmap image)
    {
        Metafile metafile = null;
        using (Graphics g = Graphics.FromImage(image))
        {
            IntPtr hDC = g.GetHdc();
            metafile = new Metafile(hDC, EmfType.EmfOnly);
            g.ReleaseHdc(hDC);
        }

        using (Graphics g = Graphics.FromImage(metafile))
        {
            g.DrawImage(image, 0, 0);
        }
        IntPtr _hEmf = metafile.GetHenhmetafile();
        uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
            EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
        byte[] _buffer = new byte[_bufferSize];
        GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC,
                EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
        DeleteEnhMetaFile(_hEmf);

        var stream = new MemoryStream();
        stream.Write(_buffer, 0, (int)_bufferSize);
        stream.Seek(0, 0);

        return stream;
    }

This one does not leave temp files behind and also avoids copying _bufferSize to a temp file only to then copy it to another buffer.
Thanks again guys.

你げ笑在眉眼 2024-10-28 13:38:15

这是一个对我有用的 Win32 GDI+ 示例(归功于 http://www.codeproject.com/Articles/6879/How-to-use-GDI-to-save-image-in-WMF-EXIF-or-EMF -fo)

Bitmap *image;
image = Bitmap::FromFile(L"in.jpg");                // read in the JPG
HDC hdc = GetDC(hwnd);                              // parent window
Metafile *metafile = new Metafile(L"out.wmf", hdc);
Graphics *graphics = new Graphics(metafile);
graphics->DrawImage(image, 0, 0, image->GetWidth(), image->GetHeight());
delete graphics; delete metafile; delete image;
ReleaseDC(hwnd, hdc);

Here's a Win32 GDI+ example that worked for me (credit to http://www.codeproject.com/Articles/6879/How-to-use-GDI-to-save-image-in-WMF-EXIF-or-EMF-fo)

Bitmap *image;
image = Bitmap::FromFile(L"in.jpg");                // read in the JPG
HDC hdc = GetDC(hwnd);                              // parent window
Metafile *metafile = new Metafile(L"out.wmf", hdc);
Graphics *graphics = new Graphics(metafile);
graphics->DrawImage(image, 0, 0, image->GetWidth(), image->GetHeight());
delete graphics; delete metafile; delete image;
ReleaseDC(hwnd, hdc);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文