Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can loadimage windows function be used to load other images other than .bmp images for instance can it load .jpg or .png files?
Posted

As you can see in the documentation[^] you can only load bitmaps, cursors, and icons.

To load a jpg-file you will need to convert it first to a bitmap. There are numerous libraries around, which can do that for you. Just search a little on CodeProject.
 
Share this answer
 
Comments
Gbenbam 14-Nov-14 5:57am    
I used to think a .jpg file too is a bitmap. Am I wrong.
nv3 14-Nov-14 8:02am    
A jpg file represents a compressed form of a bitmap. Hence it is not in bitmap format.
BillWoodruff 14-Nov-14 8:37am    
All graphic file formats encode in some way their bitmap contents, and add information internally about the nature of the encoding; that encoding may discard information to reduce file size, like .jpg does: that's why .jpg is called a "lossy" format. Other formats, like .png may not discard information: they are "lossless." In addition to whether or not a graphic file format discards information, or preserves all of it, various forms of compression are applied.

I see no problem with describing any instance of a file in any graphic file format as a "bitmap." But, keep in mind that a 'Bitmap object in .NET is really a type of data structure.
Gbenbam 14-Nov-14 9:14am    
so does that mean that the c++ loadimage function can be used to load .jpg, .png etc also.
nv3 14-Nov-14 9:31am    
As I said above: No, you cannot.
No. LoadImage cant load anything that's terribly modern. You can load bmp, .ico, .cur and that's about it. Since over 10 years now Windows has included the GDI+ subsystem, which is more or less, an object-oriented, improved version of GDI. GDI+ can load these other images without a problem - loading anything that Windows will display natively - including png, jpg, emf, wmf and a bunch of others.

Doing so is rather trivial - See below for an example of use. Make sure you link against the Gdiplus library. Also note, you just need to startup/shutdown Gdi+ once per program, which makes my example essentially a 1 line program that uses gdi+.

#include <gdiplus.h>

using namespace Gdiplus;

// BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF
HBITMAP mLoadImageFile(wchar_t *filename)
{
    HBITMAP result = NULL;
    Bitmap bitmap(filename, false);
    bitmap.GetHBITMAP(0, &result);
    return result;
}

int main()
{
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR           gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    HBITMAP mBkgImg = mLoadImage(L"forrest.jpg");

   	GdiplusShutdown(gdiplusToken);

}
 
Share this answer
 
v4
Comments
Gbenbam 14-Nov-14 16:27pm    
Now, that is really cool. Thanks a lot.
enhzflep 14-Nov-14 16:35pm    
No worries, you're welcome. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900