The C/C++ programming language is one of the most popular programming languages in the world. C is a procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. C is one of the fastest languages to operate on images, we can use text forms and we can print images by plotting pixels or by changing pixels of a bitmap. C++ Builder is a powerful C++ Code Editor that you can also use to create C apps. Your C++ Builder apps can have modern libraries like the VCL and FMX libraries to display and manipulate different types of images that code may run in different operating systems.
There are many options to display Images in C++. I think the best and easy way to display image in C++ Builder is using TImage
and loading an image into it.
Table of Contents
How to display image in C++ program using C++ Builder ?
In C++ Builder there is a great TBitmap class in VCL and TBitmap class in FMX libraries that allows you load, save, display, analyze and modify images easily. It supports bmp, png, jpg, and some other image formats.
TBitmap is an encapsulation of a bitmap. A TBitmap is a powerful graphics object used to create, manipulate and store images in memory and as files on a disk. TBitmap contains an internal image of the bitmap graphic and automatically manages realization of the palette when drawn. It also has an internal Canvas. A TBitmap can be created from a stream or a file specified by name using CreateFromStream and CreateFromFile. The image contained by the current TBitmap can be loaded or saved from or to specified streams or files. For a list of formats supported by each platform, see TBitmapCodecManager.
We can use TImage to display images and we can load an image by using LoadFromFile() method in C++ Builder FMX applications as below,
1 |
Image1->Bitmap->LoadFromFile("D:\myimage.bmp"); |
Here is an example how to use TBitmap in FMX application,
1 2 3 |
TBitmap *bmp = new TBitmap(0,0); bmp->LoadFromFile("D:\myimage.bmp"); |
Here are more Bitmap examples,
How to display image in C program using graphics library?
In some applications, you may be using old compilers that may require this graphics library. It has been may be 20 or 30 years that I haven’t used graphics library in my applications. In some C++ compilers ( mostly old ones may have graphics.h ) and IDE’s which are using MinGW and GNU C/C++ compiler you can use graphics.h library. You can download graphics.h library for C application from here : https://github.com/SagarGaniga/Graphics-Library/blob/master/graphics.h or you can download it from from http://winbgim.codecutter.org/
Here is a very simle example to display a bitmap using graphics.h,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main() { initwindow( 800, 600, "Graphic Example in C" ); readimagefile( "myimage.bmp", 10,10, 50,50 ); getch(); return 0; } |
As you see you we can use the readimagefile()
function of graphics library to display images. Here is the definition of the readimagefile()
function.
1 2 3 4 |
void readimagefile( const char* filename=NULL, int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX ); |
How to display image in C program using OpenCV?
In C and C++ you can use OpenCV library to display images. The Intel Image Processing Library(IPL) has a IplImage which is a structure to point data that contains the image elements like size, number of channels, etc. It has a pointer called IPL image header. Thus, we can allocate an image to this via cvLoadImage()
function as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <cv.h> #include <highgui.h> int main() { IplImage *img = cvLoadImage( "myimage.jpg", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "myimage" ); cvShowImage("myimage", img); cvWaitKey( 0 ); cvReleaseImage( &img ); cvDestroyAllWindows(); } |
How to display image in C program using GTK?
In GNU C / C++ we can use GTK library to display images. Simply you can use gtk_image_new_from_file()
function to load an image to a GtkWidget as below,
1 |
GtkWidget *image = gtk_image_new_from_file( "myimage.jpg" ); |
GTK Image has been explained as here officially by gtk.org. We can use gtk_new_image_from_file()
function to load image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <gtk/gtk.h> void destroy(void) { gtk_main_quit(); } int main (int argc, char *argv[] ) { GtkWidget *window, *image; gtk_init (&argc, &argv); window = gtk_window_new( GTK_WINDOW_TOPLEVEL ); image = gtk_image_new_from_file( "myimage.jpg" ); gtk_signal_connect(GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (destroy), NULL); gtk_container_add(GTK_CONTAINER (window), image); gtk_widget_show_all(window); gtk_main(); return 0; } |
How to display image in C program in a text form?
In console applications you can use ASCII characters to display images. This is a very old method but still we are using to have fun in some examples. Here are the two great examples that shows how you can use to display Mandelbrot in 2D and a 3D donut amazingly.
Although the free C++ Builder Community Edition is extremely powerful it is intended for students, beginners, and startups. If you are a regular business or do not qualify for the free community edition then you can download a free trial of the very latest full RAD Studio C++ Builder version.
C++ Builder is the easiest and fastest C and C++ IDE for building everything from simple to professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder an