Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace My {
- public class Image : Gtk.DrawingArea {
- construct {
- draw.connect (context => {
- int width = get_allocated_width();
- int height = get_allocated_height();
- uint8[] buffer;
- var res = pixbuf.scale_simple (width, width * pixbuf.height / pixbuf.width, Gdk.InterpType.BILINEAR);
- if (height < res.height)
- res = pixbuf.scale_simple (height * res.width / res.height, height, Gdk.InterpType.BILINEAR);
- res.save_to_buffer (out buffer, "png");
- var stream = new MemoryInputStream.from_data (buffer);
- var surface = new Cairo.ImageSurface.from_png_stream (data => {
- stream.read (data);
- return Cairo.Status.SUCCESS;
- });
- context.set_source_surface (surface, (width - res.width) / 2, (height - res.height) / 2);
- context.paint();
- });
- notify["location"].connect (() => {
- pixbuf = new Gdk.Pixbuf.from_stream (File.new_for_path (location).read());
- });
- }
- public Gdk.Pixbuf pixbuf { get; set; }
- public string location { get; set; }
- }
- }
- // compile with : valac sample.vala --pkg gtk+-3.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement