简体   繁体   中英

Loading Pixbuf to Image Widget in GTK#

I'm trying to load an image file from harddisk to the image widget in GTK#.I know that Pixbuf is used to represent the image.In .net i had used Bitmap b=Bitmap.from File ("c:\\windows\\file.jpg")

and assigned PictureBox=b ;

How can i do this with Image Widget

Update:

I tried

protected void OnButton2ButtonPressEvent (object o, ButtonPressEventArgs args)
    {
        var buffer = System.IO.File.ReadAllBytes ("i:\\Penguins.jpg");
        var pixbuf = new Gdk.Pixbuf (buffer);
        image103.Pixbuf = pixbuf;

   }

But it does not work.

Try this:

var buffer = System.IO.File.ReadAllBytes ("path\\to\\file");
var pixbuf = new Gdk.Pixbuf (buffer);
image.Pixbuf = pixbuf;

Also you may create a pixbuf like this:

var pixbuf = new Gdk.Pixbuf ("path\\to\\file");

But when I tried to use this constructor with path containing some russian symbols I had an exception because of wrong encoding.

Update I don't know any legacy method to set in gtk# image stretch option and I usually solve this problem with creation of new control. So right click on the project->Add->Create Widget and set the name to ImageControl . Add Image on created widget. Then edit ImageControl 's code like this:

[System.ComponentModel.ToolboxItem (true)]
public partial class ImageControl : Gtk.Bin
{

    private Pixbuf original;

    private bool resized;

    public Gdk.Pixbuf Pixbuf {
        get
        { 
            return image.Pixbuf;
        }
        set
        { 
            original = value;
            image.Pixbuf = value;
        }
    }

    public ImageControl ()
    {
        this.Build ();
    }
    protected override void OnSizeAllocated (Gdk.Rectangle allocation)
    {
        if ((image.Pixbuf != null) && (!resized)) {
            var srcWidth = original.Width;
            var srcHeight = original.Height;
            int resultWidth, resultHeight;
            ScaleRatio (srcWidth, srcHeight, allocation.Width, allocation.Height, out resultWidth, out resultHeight);
            image.Pixbuf = original.ScaleSimple (resultWidth, resultHeight, InterpType.Bilinear);
            resized = true;
        } else {
            resized = false;
            base.OnSizeAllocated (allocation);
        }
    }

    private static void ScaleRatio(int srcWidth, int srcHeight, int destWidth, int destHeight, out int resultWidth, out int resultHeight)
    {
        var widthRatio = (float)destWidth / srcWidth;
        var heigthRatio = (float)destHeight / srcHeight;

        var ratio = Math.Min(widthRatio, heigthRatio);
        resultHeight = (int)(srcHeight * ratio);
        resultWidth = (int)(srcWidth * ratio);
    }
}

Now you may set pictures with Pixbuf property of ImageControl 's widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM