2

下面是我用来从 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 放在首位,但我仍然遇到访问冲突

4

1 回答 1

0

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

IntPtr pixbuf = cairo_create (cairosurface);

gdk_pixbuf_save 的第三个参数应该是“jpeg”

也许你也应该初始化错误?

于 2012-10-01T14:08:48.003 回答