2012-10-01 41 views
2

下面是我用來從svg文件保存png的代碼。這有效,但我也需要將它保存爲jpeg。有人可以告訴我如何做到這一點?如何從開羅保存jpg

private void RasterizeSvg(string tempsvg, string rsltPath, int _width, int _height) 
    { 
     bool callSuccessful = SetDllDirectory(@"C:\ProgramDownloads\librsvg\librsvg-dev_2.32.1-1_win32\bin"); 
     if (!callSuccessful) 
     { 
      throw new Exception("Could not set DLL directory"); 
     } 
     g_type_init(); 
     IntPtr error; 
     IntPtr rsvghandle = rsvg_handle_new_from_file(tempsvg, out error); 
     if (error != IntPtr.Zero) 
     { 
      throw new Exception(Marshal.ReadInt32(error).ToString()); 
     } 
     IntPtr cairosurface = cairo_image_surface_create(cairo_format_t.CAIRO_FORMAT_RGB24, _width, _height); 
     IntPtr cairorenderer = cairo_create(cairosurface); 
     bool brslt = rsvg_handle_render_cairo(rsvghandle, cairorenderer); 

     //cairo_surface_write_to_png(cairosurface, rsltPath); 

     IntPtr pixbuf = IntPtr.Zero; 
     cairo_set_source_surface(pixbuf, cairosurface, 0, 0); 
     cairo_rectangle(pixbuf, 0, 0, _width, _height); 
     cairo_fill(pixbuf); 

     callSuccessful = gdk_pixbuf_save(pixbuf, rsltPath, "jpg", out error, __arglist("")); 
     if (!callSuccessful) 
     { 
      throw new Exception(error.ToInt32().ToString()); 
     } 
    } 

我已經改變了cairo_set_source的順序,把cairo_rectangle第一,但我仍然得到一個訪問衝突

回答

0

你傳遞一個空pixbuf的指針cairo_set_source_surface。你應該通過這樣的事情...

IntPtr pixbuf = cairo_create(cairosurface);

的第三個參數gdk_pixbuf_save應該是 「JPEG」

也許你應該初始化錯誤嗎?

+0

好的,會改變,但我甚至沒有達到那一點。我在cairo_set_source_surface上遇到訪問衝突。如果我先放cairo_rectangle,它也會拋出av – edepperson

+0

你傳遞一個空指針給它。 –

+0

好的,我將它改爲: IntPtr pixbuf = cairo_image_surface_create(cairo_format_t.CAIRO_FORMAT_RGB24,_width,_height); 但cairo_set_source仍然拋出av – edepperson