Display Images In A fixed Container

This example sets the main window to be a fixed container into which you can place images at specified positions

	MainWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL); 		//GTK_WINDOW_TOPLEVEL = Has a titlebar and border, managed by the window manager.
	
	//Get screen size
	int ScreenWidth;
	int ScreenHeight;
	GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(MainWindow));
	ScreenWidth = gdk_screen_get_width(screen);			// in pixels
	ScreenHeight = gdk_screen_get_height(screen);
	
	//Create a Fixed Container
	GtkWidget *fixed;
	fixed = gtk_fixed_new();
	gtk_widget_set_size_request(fixed, ScreenWidth, ScreenHeight);
	gtk_container_add(GTK_CONTAINER(MainWindow), fixed);
	gtk_widget_show(fixed);

	//Add an image
	GtkWidget *image;			
	image = gtk_image_new_from_file("/home/pi/projects/my_project/images/my_image.png");
	gtk_fixed_put(GTK_FIXED (fixed), image, 10, 20);		//x, y from top left
	
	
	gtk_widget_show_all(MainWindow);
	

Change an image

	gtk_image_set_from_file(GTK_IMAGE(image), "/home/pi/projects/my_project/images/my_image2.png");

Clear an image

	gtk_image_clear(GTK_IMAGE(imgDialSequence));

Delete an image

	gtk_widget_destroy(imgDialSequence);
	imgDialSequence = NULL;

You can safely used destroy on a widget that hasn’t actually been displayed yet.  You can re-create and place the widget again later after it has previously been destroyed, you’re just removing its current usage.

Paths

The path you specify is not the path referenced from the executable, it must be the actual path to the image as otherwise the image won’t be shown when running your executable by specifying its path from another location.

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *