How to show an image?

Hi!
I'm trying to write a function in C++ to show an image on the screen.
How could I do that?
I have already created a .JPG image but I was unable to use it inside a C++ software.
Please, explain me.
Thanks in advance!
You'll have to use a 3rd party library to display an image, currently C++ doesn't have anything native to the language that will do it.

What is your OS, and what compiler are you using? That will give us help in giving you some hints about what graphics libraries you can use.
Thanks for replying. I have Win7 OS and I'm using Codeblocks compiler.
If you just want to open the image in the default application (e.g. Windows Photo Viewer), you only have to call the command to start/open the image (like when you type it in the command prompt.
This could be as simple as:
1
2
3
4
5
6
7
#include <cstdlib>

int main()
{
    system("temp.jpg");
    return 0;
}


If you want to show the image inside your application, you will indeed need a third part library for graphical user interface. There are several to choose from, which one is best totally depends on your goals and preference.
wxWidgets works together with Codeblocks very nicely (www.wxwidgets.org), there is a designer build in (wxSmith). It is free and has pre-build binaries available for Windows. However the applications typically looks outdated/old.
A relevant example might be:
https://github.com/wxWidgets/wxWidgets/blob/WX_3_0_BRANCH/samples/image/

Kind regards, Nico
Note: You don't have to include the <stdlib.h> header file to use system (). At least I don't. Also, you should make sure your program is in the same directory as your image, otherwise you will need to specify the full path to it.

My OS requires the "open" in front of the image name; yours might not. I am not an expert on Windows, but you might try it and see. I believe Windows requires a "c:\\" or something like that in front of it also. And, depending on your compiler, you don't need to return 0 from main–it does it automatically.
Like this:
1
2
3
4
int main ()
{
	system ("open Desktop/test.png");
}


Good luck!
max
You don't have to include the <stdlib.h> header file to use system (). At least I don't.
What is the point in saying this? You can go to https://cplusplus.com/reference/cstdlib/system/ and see that systems exists within the cstdlib. It does not matter if it works for you without #including it; you should always #include the header that contains the functionality you need.

And, depending on your compiler, you don't need to return 0
You always will not need the "return 0;" in main; that is 100% defined behavior. No need for such qualifiers as "depending on...". I personally prefer to add it if there are other places where return is used in main, otherwise I leave it as implicit.

Sorry if I sound harsh, I can see you have good intentions.

______________________

SunriseK,
As others have mentioned, while displaying an image may seem trivial, it can be complicated because you need two major things that C++ itself doesn't provide: A windowing system which will host the image, and a way to read the image file and retrieve its image data.

Nico's suggest of wxWidgets is good; I have used it myself and I agree it gives you the essentials of what you need to get a GUI to work (such as to display an image). But any third-party library in C++ can be hard to correctly configure if you don't know what you are doing. I would search for tutorials for how to set up wxWidgets with Code Blocks.

Other than that, my advice may get some boos here: Don't use C++ for this. There are C# applications like WinForms and WPF that make loading and displaying an image quite trivial.
https://www.c-sharpcorner.com/uploadfile/mirfan00/uploaddisplay-image-in-picture-box-using-C-Sharp/
https://stackoverflow.com/questions/4332652/use-wpf-image-control-to-show-an-image-from-filesystem

You can use C++\CLI (a Microsoft .NET extension to C++) to accomplish this as well.
https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-set-pictures-at-run-time-windows-forms?view=netframeworkdesktop-4.8
Last edited on
@Ganado,
But if it requires the <stdlib.h> header file, then how come I can use system() without it??? That doesn't make sense!

For return 0, do you mean like if there's an error, you would "return EXIT_FAILURE" or something like that? Or "return 1" if that's what you prefer. Personally, I use assert() for error checking, as it actually gives an error message and the line of the program; so it's really useful for a built-in debugging tool, especially for huge programs. And you can disable it with #define NDEBUG once your code is finished.
But if it requires the <stdlib.h> header file, then how come I can use system() without it??? That doesn't make sense!

Most likely because your C++ implementation silently includes other header files in commonly used headers. For example, one implementation might include <stdlib.h>/<cstdlib> in other header files, other implementations might not.

MS/MSVC++ does that a lot, adding additional headers, which makes writing portable code a bit harder when it comes to including required headers. A PITA, but then I include the headers I know I'll need for a project.

That doesn't mean including a whole bunch of headers I don't need for a project either as a default. Adding headers you don't need won't causes problems. It is just rather sloppy IMO.

If'n I'm unsure what headers I need I do some header research.

IIRC the standard is rather iffy about what headers can be included in headers or not.
Many thanks to you all for the suggestions and the various comments!
I was already easily able to use system () to display the image using the system default application window.
I have a question: is there somewhere a list of all such windows functions, such as for example how to close that same window?
I have also downloaded wxWidgets and I will study it to see how can I integrate such software into my own application.
Bye for the moment.
SunriseK wrote:
is there somewhere a list of all such windows functions

The best place is Microsoft Docs when dealing with Windows.

The following list is the top level index for the Windows API function categories available:
https://docs.microsoft.com/en-us/windows/win32/apiindex/windows-api-list

A list of Windows 7 Command Prompt commands (about 230):
https://www.lifewire.com/list-of-windows-7-command-prompt-commands-4107370

Your question is not as simple as you believe it to be. :)
I have Win7 OS

Just so you know official MS support for Win 7 ended back in January 2021. No more bug fixes, etc.

Supposedly you can still upgrade Win 7 to Win 10 for free, if your system meets or exceeds the minimum requirements.
https://geeksadvice.com/upgrade-to-windows-10-for-free/

I've upgraded several Win 7 computers to Win 10 I purchased in the past after the official free offer had expired, though it was about 2 years ago or so. Mid-2018. It simply takes a bit of 'net bandwidth and several hours to let the upgrade chug through the process.

Win 10 is more stable than Win 7 IMO. Also, once upgraded no more "gotta buy that new OS every couple of years" merry-go-round.
I will present a counter viewpoint to Furry Guy's post (not trying to argue, just sharing a different experience).

I would hardly call Win 10 more stable than Win 7, unless you specifically get the "long-term support" branches of the product, and only ever need to stick with/support that one system.

"Windows 10" is a misnomer. It's a name in marketing only; there will never be Windows 11. The actual version you have to deal with is whether it's Windows "1607" or "1709" or "1809", "1909", etc (type in 'winver' to see your specific version). There are features only available in particular versions of 'Windows 10', and different behind-the-scenes behavior in the OS. If you have to support/verify your software on multiple types of Windows 10 systems, it can be a nightmare for compatibility. For example, some NVidia drivers require different setups depending on if you're on Windows 1607 (LTSB* 2016) vs. Windows 1809 (LTSC 2019). Driver support when a new version of Windows 10 comes out can be finicky or just incompatible. The .NET 4.7.2 version that's built-in to Windows 1809 is different than the .NET 4.7.2 version that you would install on Windows 1607. Different versions of Windows 10 then need different patching applied to them, and any non-"long term support" branch of Windows 10 goes end-of-support in like a year after release.

Essentially, Windows 10 LTSB 2016 and Windows 10 LTSC 2019 are different operating systems; they just both happen to have the same user interface on their surfaces.

...granted, I will admit probably most users won't ever have to deal with minutia of driver incompatibility and patching/testing.

*Microsoft retroactively started referring to "LTSB" as "LTSC" ("Channel" instead of "Branch"), confusing matters even more.

PS: Yes, I would avoid Windows 7 at this point like the plague, especially if you do ANYTHING financial on it, since it is end-of-life and pretty much open to attack unless you pay Microsoft for special patches.
Last edited on
Topic archived. No new replies allowed.