Frage

How do you convert a Gdk.Pixbuf (_pixbuf below) into a Windows Forms (System.Drawing) Bitmap? Below is code that uses Gtk.DotNet.Graphics, but it just gives a white image. Is there a better, more direct way, that works? I'm using C# on Mono, but I can convert if you have an answer in another language.

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);
}
War es hilfreich?

Lösung

You can use this extensions method:

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")); 
    }
}

Usage:

{
    Pixbuf pxb = new Pixbuf(pixSource);
    Bitmap bmp = pxb.ToBitmap();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top