将Gdk.Pixbuf转换为System.Drawing.Bitmap?

时间:2013-04-07 16:06:05

标签: c# gtk bitmapimage gdk pixmap

如何将Gdk.Pixbuf(下面的_pixbuf)转换为Windows窗体(System.Drawing)位图?下面是使用Gtk.DotNet.Graphics的代码,但它只是一个白色图像。是否有更好,更直接的方式?我在Mono上使用C#,但如果你有另一种语言的答案,我可以转换。

public System.Drawing.Bitmap toBitmap () {
    Gdk.Pixmap pixmap, mask;
    _pixbuf.RenderPixmapAndMask(out pixmap, out mask, 255);
    System.Drawing.Graphics graphics = Gtk.DotNet.Graphics.FromDrawable(pixmap);
    return new System.Drawing.Bitmap(width, height, graphics);
}

1 个答案:

答案 0 :(得分:2)

您可以使用此扩展方法:

using System.ComponentModel;
using System.Drawing;

public static class MyExtensions
{
    public static System.Drawing.Bitmap ToBitmap(this Pixbuf pix)
    {
        TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));

        //// Possible file formats are: "jpeg", "png", "ico" and "bmp"
        return (Bitmap)tc.ConvertFrom(pix.SaveToBuffer("jpeg")); 
    }
}

用法:

{
    Pixbuf pxb = new Pixbuf(pixSource);
    Bitmap bmp = pxb.ToBitmap();
}