I need a source code - how to use JPG-images in Visual C++?

Hello everybody!

Can you provide me with a source code - which is able to operate with JPEG-images using C++ and Win32API? (I mean without MFC or any other HUGE libraries) Also it should works on Windows 95, NT 4.0 and greater. I don't need to save images, only to load them from files and dislay them on the screen.

I wrote a small game. The .exe file size is 32Kb.
But using .bmp files means to inflate the game size.
F.e. the file size of all images in .bmp = 115Kb but in JPEG = 64Kb. Do you see the difference? Plus JPEG images looks better.
If you also provide me with a some good website about game programming technics in C++ it would be perfect and will costs more points! :)

Thank you in advance!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Why not get a copy of the IJG Jpeg Library source www.ijg.org
This can easily be compiled into a very small standallone lib file for you to use.

Alternatively you could use the windows OleLoadPicture() function (search MSDN for LoadPic for an example).

Note the second solution uses OLE dlls which are not present on a clean installation of the original Win95.
MasonokAuthor Commented:
The stream must be in BMP (bitmap), WMF (metafile), or ICO (icon) format... ? It doesn't mention about JPG.

Of couse http://www.ijg.org
supports jpg file: it is
Independedny JPEG groupe.
Other places for code:
Or PaintLib -> may be some URL's,every time migrates:
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


OleLoadPicturePath it does support jpeg files. I have written a couple of applications that loads and display JPEG files using this function and it works correctly.


IPictureDisp* p1;

CComBSTR str(_T("C:\\Image.jpg"));

HRESULT hr = OleLoadPicturePath(str,     // path to load                    NULL,  // IUnknown for aggregation               NULL,  // reserved                         NULL,                              IID_IPictureDisp,                         (void**)&p1);
Pressing the 'tab' key is not a good idea (see my last answer)

What I meant to say is....

The best way to quickly load jpegs is to use OleLoadPicturePath win32 api function.  This creates an OLE picture object and returns a pointer to an interface on that object.  The two interfaces of most use are IPictureDisp and IPicture.  IPicture has a render method, into which a dc can be passed.  OleLoadPicturePath can load jpegs, bmps, gifs and icons - all that needs changing is the string in the first parameter.

Here is some example code.

IPicture* p1;

CComBSTR str(_T("C:\\Image.jpg"));

HRESULT hr = OleLoadPicturePath(str,     // path to load                    
                    NULL,  // IUnknown for aggregation
                    NULL,  // reserved
                             NULL,  // back colour
                                 IID_IPicture,  // interface to retrieve - must match type of p1
                             (void**)&p1);      // pointer to interface

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MasonokAuthor Commented:

Could you please create a small project based on Win32 Application (wizard) which is display "Image.jpg" somewhere on client area using OleLoadPicturePath in  WM_PAINT section.

and send the code of this project to : masonok@yahoo.com

I'll post here rather than emailing so that everyone can read it...

There's no need for me to write something, as there is already just the application that you are looking for at the Microsoft support site.  Follow this link: http://support.microsoft.com/support/kb/articles/Q218/9/72.ASP
MasonokAuthor Commented:
It's exactly what I need!!!

Thanks a lot!
HELLO???? Did any body read my first post???

"use the windows OleLoadPicture() function (search MSDN for LoadPic for an example)"

Sorry if that wasn't clear enough for you.


Sorry, didn't mean to steal your glory!

Your answer relies on OleLoadPicture() which relies on access to a stream object.  The MSDN says that the stream must be in BMP (bitmap), WMF (metafile), or ICO (icon) format.

First, I think Mansonok wanted to load directly from a disk without messing around with streams, so OleLoadPicturePath() is better.

Second, we are dealing with jpgs that OleLoadPicture() doesn't support.  Actually, I think this is rubbish - I'd be most supprised if you can't use OleLoadPicture() to get a jpg out of a stream, as all these calls are implemented using the ole picture object.

I suggested that OleLoadPicturePath works with jpeg files (see my first comment). You first proposed an answer when I believe you should just give a comment. Then if Masonok wanted more details he just had to ask.

Fair point and sorry - I'm new to this (only started today) and didn't realise how it all works...
No problem :-) welcome to EE!
MasonokAuthor Commented:
I hope I am right with my decision. Inspite of that I gave points to NickAtBOM I want to thank ALL of YOU!
I am not an expert in C++ - I am a beginner (especially in OLE COM and stuff) so I look for a clear and more understandable answer. :)

Look at my question : Can you provide me with a SOURCE CODE - which is able to operate with JPEG-images using C++ and Win32API?

Because of : From MSDN
The stream must be in BMP (bitmap), WMF (metafile), or ICO (icon) format... ? It doesn't mention about JPG - I was afraid that the answer is not correct... so I didn't go forward...
Also without an explanation I had no idea how to use OlePictureLoadPath. S0rry! :)

Look at my Math
robpitt + gocarlos = NickAtBOM + NickAtBOM
(Sample) + function = function + direct link to a sample
So only NickAtBOM gave me the whole answer!

Please do not forget that person who asks a question may not be an expert! And in most cases it is not enough to say : Use this function! I believe that the small simple sample - always will be helpful!

Anyway I want to thank all of you because this dialog was VERY useful and helpful for me!!!!!!!!!!!! And I hope I can rely on your help in the future!

C U later! :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.