2014-05-08 80 views
0

我正在尝试将.emf文件渲染为给定大小的位图。将.emf文件渲染为位图

这是我的代码中,我使用此答案为基:How do I resize (shrink) an EMF (Metafile) in .Net?

public void SetEmfFromBitmap(string emfPath, Size size) 
    { 
     if (emfPath.Contains(".emf")) 
     { 
      using (var source = new Metafile(emfPath)) 
      using (var target = new Bitmap(size.Width, size.Height)) 
      using (var g = Graphics.FromImage(target)) 
      { 
       g.DrawImage(source, 0, 0, size.Width, size.Height); 
       target.Save("c:\\temp\\Month_Calendar.png", ImageFormat.Png); //this works.. 
       this.Image = target; //but this doesnt??? 
      } 
     } 
     else 
     { 
      throw new ArgumentException("Invalid path to .emf file: " + emfPath); 
     } 
    } 

约的方法属于被从PictureBox的派生类。这就是为什么我设置this.Image(这是一个位图)。但是我能看到的是我Winforms应用程序中的白色背景的红色十字。

那么,位图对象有什么问题?

回答

0

我自己找到了原因:由于使用using语句,目标位图被丢弃。答案是使用this.Image =(位图)target.Clone();代替。