2011-11-16 33 views
1

我尝试将JPG图像转换成矢量format.I尝试实现这个代码,但它是通过exeception如何JPG,位图格式的图像转换成矢量格式在C#

public void Run() 
     { 
      Control c = new Control();   
      Graphics grfx = c.CreateGraphics(); 
      //ReadImage(ImageName) method return the Image to Byte Array 
      MemoryStream ms = new MemoryStream(ReadImage(@"E:\Temp\1.jpg")); 
      IntPtr ipHdc = grfx.GetHdc(); 
      Metafile mf = new Metafile(ms,ipHdc); 
      grfx.ReleaseHdc(ipHdc); 
      grfx.Dispose(); 
      grfx = Graphics.FromImage(mf); 
      mf.Save(@"E:\Temp\file.wmf", ImageFormat.Wmf);//Get Exception on this line 
      grfx.Dispose(); 
     } 

Exeception是:A GDI +发生通用错误。 请验证我的代码,我犯了错误。 由于提前

+0

而在哪一行源代码上抛出异常? – dthorpe

+0

在这条线上,我得到了怀疑 mf.Save(@“E:\ Temp \ file.wmf”,ImageFormat.Wmf); –

回答

1

我在申请前得到了同样的错误的问题发生becouse写入到磁盘文件,从而你

E:\Temp\file.wmf 

检查写文件权限的!我的目录是网络中的映射内存,所以我必须连接目录与传递和用户名。确保父目录存在,并确保路径包括文件名和扩展名。如果它不工作尝试运行程序作为管理员

3

该代码不能工作:据我所知的Metafile构造预期已经包含了一些图元文件数据流(即” .wmf'-文件)或为空(新图元文件)

您应该创建一个新的图元文件,创建一个图形上下文,将jpeg图像加载到一个单独的对象中,并在元文件上下文中绘制该对象。然后,您可以将图元文件保存为“.wmf”文件。

我自己没有这样做,但我发现了一个article on CodeProject,它解释了许多有关元文件创建的(棘手)细节。

但请注意,这不是一个“真正”的位图矢量转换。它只是将位图嵌入到“.wmf”容器中。例如,如果尝试调整其大小,则会得到与原始jpeg-Image相同的结果(即没有“平滑”缩放)。

+0

这个告诫确实让我免于浪费时间......追捕继续! –

0
public string Main(Bitmap image) 
     { 
      string str = ""; 
      try 
      { 

       int width = image.Width; 
       int height = image.Height; 

       Graphics offScreenBufferGraphics; 
       Metafile m; 
       using (MemoryStream stream = new MemoryStream()) 
       { 
        using (offScreenBufferGraphics = Graphics.FromImage(image)) 
        { 
         IntPtr deviceContextHandle = offScreenBufferGraphics.GetHdc(); 
         m = new Metafile(
         stream, 
         deviceContextHandle, 
         new RectangleF(0, 0, width, height), 
         MetafileFrameUnit.Pixel, 
         EmfType.EmfPlusOnly); 
         offScreenBufferGraphics.ReleaseHdc(); 
        } 
       } 

       using (Graphics g = Graphics.FromImage(m)) 
       { 
        // Set everything to high quality and Draw image 
        g.SmoothingMode = SmoothingMode.HighQuality; 
        g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
        g.PixelOffsetMode = PixelOffsetMode.HighQuality; 
        g.CompositingQuality = CompositingQuality.HighQuality; 
        MetafileHeader metafileHeader = m.GetMetafileHeader(); 
        g.ScaleTransform(
         metafileHeader.DpiX/g.DpiX, 
         metafileHeader.DpiY/g.DpiY); 
        g.PageUnit = GraphicsUnit.Pixel; 
        g.SetClip(new RectangleF(0, 0, width, height)); 
        Point ulCorner = new Point(0, 0); 
        g.DrawImage(image, 0, 0, width, height); 


       } 

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

       // Export metafile to an image file 
       CopyEnhMetaFile(iptrMetafileHandle, @"c:\\Ginko-Bilobatest1234.wmf"); 
       str = "wmf image successfully generated."; 
      } 
      catch (Exception ex) 
      { 
       str = ex.InnerException + ex.Message; 
      } 
      return str; 
      // Delete the metafile from memory 
      // DeleteEnhMetaFile(iptrMetafileHandle); 
     } 
相关问题