Advertisement
rotrevrep

Custom resizable Gtk.Image

Jan 11th, 2016
3,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 1.06 KB | None | 0 0
  1. namespace My {
  2.     public class Image : Gtk.DrawingArea {
  3.         construct {
  4.             draw.connect (context => {
  5.                 int width = get_allocated_width();
  6.                 int height = get_allocated_height();
  7.                 uint8[] buffer;
  8.                 var res = pixbuf.scale_simple (width, width * pixbuf.height / pixbuf.width, Gdk.InterpType.BILINEAR);
  9.                 if (height < res.height)
  10.                     res = pixbuf.scale_simple (height * res.width / res.height, height, Gdk.InterpType.BILINEAR);
  11.                 res.save_to_buffer (out buffer, "png");
  12.                 var stream = new MemoryInputStream.from_data (buffer);
  13.                 var surface = new Cairo.ImageSurface.from_png_stream (data => {
  14.                     stream.read (data);
  15.                     return Cairo.Status.SUCCESS;
  16.                 });
  17.                 context.set_source_surface (surface, (width - res.width) / 2, (height - res.height) / 2);
  18.                 context.paint();
  19.             });
  20.             notify["location"].connect (() => {
  21.                 pixbuf = new Gdk.Pixbuf.from_stream (File.new_for_path (location).read());
  22.             });
  23.         }
  24.        
  25.         public Gdk.Pixbuf pixbuf { get; set; }
  26.         public string location { get; set; }
  27.     }
  28. }
  29.  
  30. // compile with : valac sample.vala --pkg gtk+-3.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement