如何从开罗中保存jpg

时间:2012-10-01 13:53:15

标签: c# svg gtk cairo librsvg

以下是我用来从svg文件保存png的代码。这有效,但我还需要将它保存为jpeg。有人可以告诉我如何做到这一点吗?

    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放了,但我仍然遇到了访问冲突

1 个答案:

答案 0 :(得分:0)

您将一个空的pixbuf指针传递给cairo_set_source_surface。你应该传递这样的东西......

IntPtr pixbuf = cairo_create(cairosurface);

gdk_pixbuf_save的第三个参数应为“jpeg”

也许你应该初始化错误?