How can we display image in visual C++ console application? The image format can be of any types like bmp, jpeg, tiff, and so on. I want to open a new window and display the image in that window. I don't want to use others' libraries like openCV and so on. Thanks!
-
1Be more specific. Do you want to use the built in Windows picture viewer? Convert a picture to ascii art? What? – 001 Dec 28 '16 at 22:03
-
Do you want to open a window (that is possible from a console application - but it's tricky) and display the image in that? Be a lot more specific about what you are trying to achieve. – Martin Bonner supports Monica Dec 28 '16 at 22:05
-
Thank you guys! Yes I want to open a window and display image in that window. – Nhorang Dec 28 '16 at 22:07
-
If you want to open a new window and display a bitmap in it, it's not too difficult, but perhaps too long for an SO answer . You will need to use the Win32 API - this project of mine https://bitbucket.org/neilb/nclip does to display any bitmap stored in the clipboard. – Dec 28 '16 at 22:44
2 Answers
You can very easily, trivially, display an image from any Windows application.
For example, you can use the C++ library's system
function to launch mspaint.exe
with the image you want. Or you can use an API function such as ShellExecute
to launch an image file in the viewer that's associated with the filename extension.
In a console application system
is particularly nice since you avoid an extra console window popping up.
Displaying an image directly in a console window is a different kettle of fish, so technically challenging that one may as well say it's practically impossible.
That has nothing to do with the application having console or GUI subsystem, but with the fact that a console window is managed for you, as I recall by a different dedicated process.
What you can do if you want that effect (which was strongly indicated by the original question), is to put a borderless window on top of the console window. Maybe, if that works, make the console window owner. But you want to move that on-top window along with the console window, which I would guess involves some complexity.

- 142,714
- 15
- 209
- 331
-
-
For Windows, [ShellExecute](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx) might be a better option. – 001 Dec 28 '16 at 22:14
-
-
Thank you guys. But, what I have is not actual image but just a pixel data from my program and want to plot those values as image in new window not in actual console. – Nhorang Dec 28 '16 at 22:20
-
@NorxangLama: Over in *nix-land you could generate a text file representation of the pixel data, in the form of a C array constant. That format is or was used for icons. But Windows doesn't support that format. Using Direct2D is a PITA, and the old API had only three ways to store an image file: do-it-yourself, with e.g. code for BMP available for copying from the Windows docs; or store as WMF via API function for that, with some crucial parts undocumented; or use the COM-based OLE Image functionality. I recommend you use a 3rd party library such as PNG-writer. – Cheers and hth. - Alf Dec 28 '16 at 22:25
-
**OR**, just implement your own window. You can do things efficiently by referencing your data from a [device indendent bitmap structure](https://msdn.microsoft.com/en-us/library/windows/desktop/dd162974(v=vs.85).aspx). – Cheers and hth. - Alf Dec 28 '16 at 22:30
The console is a text-based environment and it won't be possible to display jpgs/bmp. You need to use a GUI library that creates windows (like WPF if you're using Windows) to display images.
You're already using others' libraries by using C++ and the console. Using libraries is what programming is all about.
You can actually open a WPF window from console application using this: How to start the WPF window from console programmatically?

- 1
- 1

- 4,608
- 3
- 25
- 41
-
Thank you Michael. I know that I am using C++ standard libraries but what I meant was I don't want use libraries like openCV and others. And, As Martin asked I want to open a new window and display image in that window. – Nhorang Dec 28 '16 at 22:11
-
Updated my answer. You can use the paint.exe application to display your image or create your own image viewer. – Michael Fulton Dec 28 '16 at 23:34