樹心幽徑

« 20180612在GTK視窗的Drawing Area顯示影像並偵測滑鼠按鍵位置(gtk9.c) | Main | 20180629openssl操作 »

20180613在GTK視窗顯示縮放到和視窗同樣大小的影像(gtk9b.c)
2018/06/13,23:17

(1)GTK視窗顯示縮放到和視窗同樣大小的影像(gtk9b.c)


treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ cat gtk9b.c
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
int main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *image;

  gtk_init (&argc, &argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  image = gtk_image_new_from_file("map.png");

  gtk_container_add (GTK_CONTAINER (window), image);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit) , NULL);
  gtk_widget_set_size_request (window, 1024,768);

  GdkPixbuf *pixbuf =    gtk_image_get_pixbuf(GTK_IMAGE(image));
  if (pixbuf == NULL)
    {
        g_printerr("Failed to resize image\n");
        return 1;
    }
  int width, height;
  gtk_window_get_size(GTK_WINDOW(window), &width, &height);
  pixbuf = gdk_pixbuf_scale_simple(pixbuf, width, height, GDK_INTERP_BILINEAR);
  gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);

  gtk_widget_show_all (window);
  gtk_main();
  return 0;
}

 

(2)編譯並執行

treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ gcc gtk9b.c -Wall `pkg-config --cflags --libs gtk+-3.0` -export-dynamic
treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ ./a.out

alt

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType