Advertisement
Guest User

Gtkmm Show Image MainWindow

a guest
Feb 24th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <gtkmm.h>
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5. #include <vector>
  6.  
  7. class MainWindow
  8. :
  9.     public Gtk::Window
  10. {
  11. public:
  12.  
  13.     MainWindow();
  14.     virtual ~MainWindow() {}
  15.  
  16. private:
  17.  
  18.     Gtk::Image image;
  19.     Glib::RefPtr<Gdk::Pixbuf> ref_orig, ref_dest;
  20.  
  21. };
  22.  
  23. MainWindow::MainWindow()
  24. :
  25.     ref_orig (Gdk::Pixbuf::create_from_file ("./image.png"))
  26. {
  27.     set_title ("Show Image");
  28.     set_position (Gtk::WIN_POS_CENTER);
  29.  
  30.     const guint8* raw = ref_orig->get_pixels();
  31.     const std::vector<guint8> image_pixels (raw, raw + ref_orig->get_byte_length());
  32.  
  33.     ref_dest =
  34.         Gdk::Pixbuf::create_from_data (
  35.             image_pixels.data(), Gdk::COLORSPACE_RGB,
  36.             ref_orig->get_has_alpha(), ref_orig->get_bits_per_sample(),
  37.             ref_orig->get_width(), ref_orig->get_height(), ref_orig->get_rowstride());
  38.  
  39.     image.set (ref_dest);
  40.  
  41.     add (image);
  42.  
  43.     show_all_children();
  44. }
  45.  
  46. int main (int argc, char *argv[])
  47. {
  48.     Glib::RefPtr<Gtk::Application> app = Gtk::Application::create();
  49.  
  50.     MainWindow main_window;
  51.    
  52.     return app->run (main_window);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement