如何在metaFile的左上角绘制

时间:2014-11-26 02:19:23

标签: c# gdi+

我在MetaFile中绘制一条简单的Line:

  var g = CreateGraphics(); 
        var img = new Metafile(path, g.GetHdc()); 
        var ig = Graphics.FromImage(img);

ig.DrawLine(new Pen(Color.Black, 2), 0, 0, 300, 0);

并且意识到,我试图将这个metaFile保存为这样的一个例子:

var target = new Bitmap(200, 100);
var g2 = Graphics.FromImage(target);
g2.DrawImage(source,0,0);// source is my MetaFile

但该线不在图像的左上方:( 为什么以及如何做到这一点?

2 个答案:

答案 0 :(得分:0)

尝试在将代码保存到图像之前,先将代码中使用的Graphics类置于其中:

 using( Graphics graphics = Graphics.FromImage( metafile ) )
 {
     graphics.DrawLine(new Pen(Color.Black, 2), 0, 0, 300, 0);
 } // right here in this point, the 'using' keyword launches graphics.Dispose(),
   // which is very importante when using graphics

答案 1 :(得分:0)

谢谢你,我发现我的理解是:

var img = new Metafile(path, g.GetHdc(), new Rectangle(0, 0, 101, 101), MetafileFrameUnit.Pixel);