将位图转换为图元文件 (.emf)

发布于 2024-11-03 16:02:03 字数 1494 浏览 0 评论 0原文

我有一个位图(.bmp)文件,我想将其转换为图元文件(.emf)格式。目前我正在使用这段代码:

        Metafile metafile;
        using (MemoryStream stream = new MemoryStream())
        using (Graphics rtfBoxGraphics = m_ActiveTab.Controls[0].CreateGraphics())
        {
          IntPtr pDeviceContext = rtfBoxGraphics.GetHdc();

          metafile = new Metafile(stream, pDeviceContext);
          using (Graphics imageGraphics = Graphics.FromImage(metafile))
          {               
            imageGraphics.DrawImageUnscaled(bmp1, new Rectangle(0, 0,bmp1.Width, bmp1.Height));     
          }
          rtfBoxGraphics.ReleaseHdc(pDeviceContext);
        }

        // Get a handle to the metafile
        IntPtr iptrMetafileHandle = metafile.GetHenhmetafile();

        // Export metafile to an image file
        CopyEnhMetaFile(iptrMetafileHandle, @"e:\Test\test4.emf");

        // Delete the metafile from memory
        DeleteEnhMetaFile(iptrMetafileHandle);


[DllImport("gdi32.dll")]
static extern IntPtr CopyEnhMetaFile(  // Copy EMF to file
  IntPtr hemfSrc,   // Handle to EMF
  String lpszFile // File
);

[DllImport("gdi32.dll")]
static extern int DeleteEnhMetaFile(  // Delete EMF
  IntPtr hemf // Handle to EMF
);

为什么这段代码不能正常工作?请向我提供将位图文件转换为图元文件(特别是 .emf)文件的完整代码。 或者 如果可能的话请告诉我如何以图元文件格式保存图形对象。 假设我现在有了这段代码,

Graphics NewGraphicsObj = m_ActiveTab.Controls[0].CreateGraphics();

我想将这个 NewGraphicsObj 保存到图元文件中。

任何人对这个主题有帮助,

提前致谢...

I have a bitmap(.bmp) file and I want to convert it into metafile(.emf) format. Currently I am using this code:

        Metafile metafile;
        using (MemoryStream stream = new MemoryStream())
        using (Graphics rtfBoxGraphics = m_ActiveTab.Controls[0].CreateGraphics())
        {
          IntPtr pDeviceContext = rtfBoxGraphics.GetHdc();

          metafile = new Metafile(stream, pDeviceContext);
          using (Graphics imageGraphics = Graphics.FromImage(metafile))
          {               
            imageGraphics.DrawImageUnscaled(bmp1, new Rectangle(0, 0,bmp1.Width, bmp1.Height));     
          }
          rtfBoxGraphics.ReleaseHdc(pDeviceContext);
        }

        // Get a handle to the metafile
        IntPtr iptrMetafileHandle = metafile.GetHenhmetafile();

        // Export metafile to an image file
        CopyEnhMetaFile(iptrMetafileHandle, @"e:\Test\test4.emf");

        // Delete the metafile from memory
        DeleteEnhMetaFile(iptrMetafileHandle);


[DllImport("gdi32.dll")]
static extern IntPtr CopyEnhMetaFile(  // Copy EMF to file
  IntPtr hemfSrc,   // Handle to EMF
  String lpszFile // File
);

[DllImport("gdi32.dll")]
static extern int DeleteEnhMetaFile(  // Delete EMF
  IntPtr hemf // Handle to EMF
);

Why is this code not work properly? Please provide me full code to convert a bitmap file to metafile(specially .emf) file.
or
If possible then tell me how can I save a graphics object in metafile format.
Suppose I have this code

Graphics NewGraphicsObj = m_ActiveTab.Controls[0].CreateGraphics();

now I want to save this NewGraphicsObj into a metafile.

Any one help on this topic

Thanks in advance...

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文