2011-11-16 54 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); 
     } 
相關問題