0

最近我在用元文件绘制东西时发现了一个错误。现在我不确定我是否做错了什么,或者元文件本身的绘制中是否存在错误:

在通过 PlayEnhMetafile 在另一个元文件本身上绘制的元文件上绘制图像时,我会在很远的下方或右侧丢失图像。我想这与屏幕坐标有关(我运行双屏 1280*1024,所以 2560*1024),因为图像开始消失的底部通道约为 500。

这是我为更具体地向您展示问题而创建的一些示例代码。(您可以使用此代码替换新创建的 Windows C# 项目的 Form1.cs 并在其上放置一个按钮)

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;

namespace MetaFileDrawing
{ 
 public partial class Form1
  : Form
 { 
  [DllImport("gdi32.dll", SetLastError = true)]
  private static extern bool PlayEnhMetaFile(IntPtr hdc, IntPtr hEmf, ref Rectangle rectangle);

  [DllImport("gdi32.dll", SetLastError = true)]
  public static extern bool DeleteObject(IntPtr hGdiObj);

  public Form1()
  {
   InitializeComponent();
  }

  /// <summary>
  /// Creates the sub-metafile where the actual drawing is done (and the problems occur).
  /// </summary>
  private Metafile GetSubMetafile()
  {
   Metafile metafile = null;
   using(Graphics controlGraphics = this.CreateGraphics())
   {
    using(MemoryStream memoryStream = new MemoryStream())
    {
     metafile = new Metafile(memoryStream, controlGraphics.GetHdc(), EmfType.EmfOnly, string.Empty);

     using(Graphics metafileGraphics = Graphics.FromImage(metafile))
     {
      Bitmap bitmap = new Bitmap("Fibonacci.png");
      // Draw the image 3 times... if pushed to far down, it wont show up?
      metafileGraphics.DrawRectangle(Pens.Yellow, new Rectangle(0, 0, 40, 1200));
      metafileGraphics.DrawImage(bitmap, new Point(0, 0));
      metafileGraphics.DrawImage(bitmap, new Point(10, 950));
      metafileGraphics.DrawImage(bitmap, new Point(20, 1150));
     }
    }
    controlGraphics.ReleaseHdc();
   }
   return metafile;
  }

  /// <summary>
  /// Creates and draws the metafile.
  /// </summary>
  private void DrawMetafile()
  {
   using(Graphics controlGraphics = this.CreateGraphics())
   {    
    using(MemoryStream memoryStream = new MemoryStream())
    {
     // EmfType.EmfOnly is a restriction defined by my project limitations
     Metafile metafile = new Metafile(memoryStream, controlGraphics.GetHdc(), EmfType.EmfOnly, string.Empty);

     using(Graphics metafileGraphics = Graphics.FromImage(metafile))
     {
      // A large red rect for orientation
      metafileGraphics.DrawRectangle(Pens.Red, new Rectangle(0, 0, 4000, 4000));

      // Create the sub metafile
      Metafile subMetafile = GetSubMetafile();

      // To check, draw the subMetafile with DrawImage (works fine, the inlined image is drawn 3 times)
      metafileGraphics.DrawImage(subMetafile, new Point(10, 0));

      // On the right side, draw the sub metafile using PlayEnhMetaFile (dont work correctly, only 2 images)
      IntPtr hMetafile = subMetafile.GetHenhmetafile();
      Rectangle rectangle1 = new Rectangle(100, 0, 170, 1230);
      PlayEnhMetaFile(metafileGraphics.GetHdc(), hMetafile, ref rectangle1);
      metafileGraphics.ReleaseHdc();
      DeleteObject(hMetafile);
     }

     metafile.Save("Output.emf");
    }
    controlGraphics.ReleaseHdc();
   }
  }

  private void button1_Click(object sender, EventArgs e)
  {
   DrawMetafile();
  }
 }
}

如您所见,使用 PlayEnhMetaFile 函数会导致我丢失三个图像之一。有任何想法吗?

4

1 回答 1

0

我使用 C++ 遇到了同样的问题,并找到了两种解决方法。我可以使用 GDI+ 附加到元文件并播放它,但坐标略有偏差。我目前使用的更复杂的替代方法是使用 EnumEnhMetaFile,并手动执行 bitblt/stretchblt/stretchdibits 调用,这似乎可行。如果您找到更好的解决方案,请告诉我。

于 2011-05-13T11:46:59.010 回答