Gtk-based imshow: reading and displaying an image gtk

gtk achieve imshow, most naive approach is used to read an image of the component gtk, then show up; consider subsequent images in other ways, for example, read stb image display GTK. When GDI previously achieved imshow is based on this idea, to be able to show up to the confidence in myself.

Official Code

gtk official document provides an example of this, and consistent show out of the original image and size.

#include <gtk/gtk.h>

static void activate (GtkApplication *app, gpointer user_data)
{
    GtkWidget *window;
    GtkWidget *image;

    /*Create a window with a title and a default size*/
    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Welcome to GNOME");
    //gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);

    const char* im_pth = "/home/zz/work/libfc/imgs/Lena.png";
    image = gtk_image_new_from_file (im_pth);

    gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (image));

    gtk_widget_show_all (GTK_WIDGET (window));
}


int main (int argc, char **argv)
{
    GtkApplication *app;
    int status;

    app = gtk_application_new ("org.gtk.example",G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
    status = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);
    return status;
}

effect:

Simple package

Imshow from the user in terms of, ta only need to pass the image path, Image Title, it should be displaying the results. The above-described code can be encapsulated. The use of global variables my_window, because the callback function can not pass additional parameters, and the follow-up window to give more binding parameters.

#include <gtk/gtk.h>
#include <stdlib.h>

typedef struct MyWindow {
    const char* im_pth;
    const char* title;
} MyWindow;

MyWindow my_window;

static void activate (GtkApplication *app, gpointer user_data)
{
    GtkWidget *window;
    GtkWidget *image;

    /*Create a window with a title and a default size*/
    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), my_window.title);
    //gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);

    image = gtk_image_new_from_file (my_window.im_pth);

    gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (image));

    gtk_widget_show_all (GTK_WIDGET (window));
}

void show_image_gtk_temp()
{
    GtkApplication *app;
    int status;

    app = gtk_application_new ("org.gtk.example",G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
    status = g_application_run (G_APPLICATION (app), 0, NULL);
    g_object_unref (app);
}

void gtk_show_image_v1(const char* im_pth, const char* title) {
   
    my_window.im_pth = im_pth;
    my_window.title = title;

    show_image_gtk_temp();
}

int main() {
    const char* im_pth = "/home/zz/work/libfc/imgs/fruits.jpg";
    const char* title = "fruit";
    gtk_show_image_v1(im_pth, title);

    return 0;
}

reference

https://developer.gnome.org/gnome-devel-demos/stable/image.c.html.en

Platform Demos in C

Guess you like

Origin www.cnblogs.com/zjutzz/p/10959816.html