The following code works correctly under Windows XP:
CImage image;
RECT destRect;
int nResource = 10;
CResourceStream stream(0, MAKEINTRESOURCE(nResource), _T("JPEG"));
HRESULT hr = image.Load(&stream);
image.Draw(hDC, destRect);
But on Windows 7 image.Load returns E_FAIL though creating CResourceStream reads JPEG file from resources correctly.
Debugging gives the following result:
GdipCreateBitmapFromStream returns InvalidParameter.
What the problem can be?
JPEG is a custom category in resource file.
At the end I used this solution from codeproject:
http://www.codeproject.com/KB/GDI-plus/cgdiplusbitmap.aspx
It is a thin wrapper for GDI+ which is able to load JPEG files (and others) under Windows 7 perfectly.
I believe you should use "JPG" not "JPEG".
Related
I am trying to load an image resource using the LoadImageA() function, yet it doesn't work and I don't understand why.
Here's a bit of my code :
bool isRessource = IS_INTRESOURCE(107);
// Load the resource to the HGLOBAL.
HGLOBAL imageResDataHandle = LoadImageA(
NULL,
MAKEINTRESOURCEA(107),
IMAGE_BITMAP,
0,
0,
LR_SHARED
);
HRESULT hr = (imageResDataHandle ? S_OK : E_FAIL);
The image I want to load is a bitmap saved in the resources, and represented as such within resources.h:
#define IDB_BITMAP1 107
When I execute the code, isRessource is equal to true, yet hr is equal to E_FAIL.
Any idea as to why this is happening? I am using Visual Studio 2019, and I made the image using Gimp.
After making the same image with the same format on another application (I used "Krita") and importing it again, the image finally loads with the same code (I only changed the reference to the resource). I guess that all types of bitmaps made from Gimp won't work in Visual Studio (I tried most formats of bitmaps from Gimp).
The first link searched with LoadImage gimp as a keyword is enough to answer this question.
This is some useful information:
The bitmap exported by GIMP has a broken header. Specifically, the
code seems to not write the RGBA masks, which AFAIK are not optional
in a BITMAPV5HEADER. This misaligns and changes the size of the entire
extended header, incidentally making it look like a BITMAPV4HEADER,
which explains why most programs will still open it fine. Without
having done any testing, I'd guess LoadImage() is more picky about the
values in this extended header; returning NULL is how it indicates
failure.
By the way, when you import a bitmap, the system does not remind you that the format of the image is unknown?
Like:
After testing, use LoadImage to load such an image will return NULL, and GetLastError will also return 0.
I am trying to load some bitmap images with my application to do some comparisons whose created using ImageMagick using following command:
C:\Program Files\ImageMagick-7.0.6-Q16>Convert << SOURCE BMP >> -crop -650 << DEST BMP >>
Above source bitmap image loads fine with LoadImage API Function, but the image created by ImageMagick (destination bitmap) fails to load.
How I am using LoadImage Function:
HBitmapRight = (HBITMAP)LoadImage(nullptr, L"C:\\DEST.bmp", IMAGE_BITMAP, NULL, NULL, LR_LOADFROMFILE);
HBitmapRight = (HBITMAP)LoadImage(nullptr, L"C:\\SOURCE.bmp", IMAGE_BITMAP, NULL, NULL, LR_LOADFROMFILE);
I used GetLastError, but it returned ERROR_SUCCESS.
Upon my research, I found this answer and so, this maybe a encoding issue or something similar.
Unfortunately I cannot use any tools like MS Paint or Photoshop, but I only can use ImageMagick because this comparison is done using command line without user interaction.
Is it possible to set any special encoding flags to be used with ImageMagick, so bitmap images creating by it can be compatible with LoadImage?
I am running this application in my Windows 7 64-bit machine and installed ImageMagick is also the latest 64-bit version of it. Compiler is MS Visual C++
2017.
Those are the lines that causes the issue:
<Mask>
<Red/>
<Green/>
<Blue/>
<Alpha/>
</Mask>
<ColorSpaceType/>
<CIEXYZEndPoints>
<Red>
<X/>
<Y/>
<Z/>
</Red>
<Green>
<X/>
<Y/>
<Z/>
</Green>
<Blue>
<X/>
<Y/>
<Z/>
</Blue>
</CIEXYZEndPoints>
<Intent/>
These lines are in XML metadata of ImageMagick created image. I checked with source image, it doesn't have those lines.
Please help me to resolve this issue without user interaction.
I fixed the problem by changing my ImageMagick command to use specific version of Bitmap format like this:
C:\Program Files\ImageMagick-7.0.6-Q16>Convert << SOURCE BMP >> -define bmp:format=bmp3 -crop -650 << DEST BMP >>
Now LoadImage API function works fine with ImageMagick created bitmap images.
I'm developing a browser plugin (npapi) using FireBreath, and in it I'm trying to embed images which I need to load at runtime.
I've been search, reading and experimenting for two days now but I can't seem to make it work.
This is the code I ended up with:
HMODULE hModule;
LPCWSTR path = L"nptest.dll";
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, path, &hModule);
LPTSTR resourceName = MAKEINTRESOURCE(106);
HBITMAP bitmap = (HBITMAP) LoadImage(hModule, resourceName, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
The loaded bitmap seems to be empty after the execution of this code, and I can't figure out why.
At first I tried to load a PNG image, and later realized that PNGs are not supported, so I just created a BMP file using the resource editor in VS2010.
I know for sure that the BMP (with id 106) is in the compiled DLL because I used PE Explorer to check inside the DLL.
Any ideas as for why it fails to load the image resource?
Thanks a lot.
I have a simple question. I use SDL and SDL_image in my c++ program and image loading is fine from a single png file.
SDL_Surface *dot = NULL;
dot = load_image("dot.png");
But how can I load the png file if I add it to the resources? so I don't want to store in a png file next to the exe. Is it possible to load from the resources?
Tried
dot = load_image(MAKEINTRESOURCE(IDB_PNG1));
but it didn't work.
It is fully possible to load an image or something else into SDL from a windows resource. To do this you need to get the raw data and pass it to the appropriate RWOPS.
HMODULE hModule = GetModuleHandle(_T("myapp.exe"));
HRSRC hWhite = FindResource(hModule, MAKEINTRESOURCE(IDB_WHITE_PNG), _T("PNG"));
unsigned int white_size = SizeofResource(hModule, hWhite);
HGLOBAL hgWhite = LoadResource(hModule, hWhite);
unsigned char* white_data = (unsigned char*)LockResource(hgWhite);
SDL_Surface* white = IMG_Load_RW(SDL_RWFromConstMem(white_data, white_size), 1);
This assumes that you have something similar in your *.rc file:
IDB_WHITE_PNG PNG "White.png"
According to the MAKEINTRESOURCE documentation :
The return value should be passed only to functions which explicitly indicate that they accept MAKEINTRESOURCE as a parameter.
You don't give the content of load_image (BTW, please include the content of the functions you use in your question, you'll get better answers ...) but I bet it's not using its parameter to call one of the Windows SDK functions which accept MAKEINTRESOURCE ... as far as I know these resources are supposed to hold some specific Windows UI data like mouse cursors, icons, etc.. for use with Windows functions, not with other libraries like SDL, so I'm not surprised it doesn't work.
How can I load a jpg into a CBitmap where I am using visual c++ 6.0 and don't have access to CImage?
thx
I would simply call OleLoadPicturePath api to load not just jpg but other common formats also.
After loading it into the IPicture object you can call get_Handle to get the handle to HBITMAP.
Thanks
There's no native way I know of. I've always used FreeImage for JPGs and PNGs - it's robust and there's example code in the FAQ on how to load a PNG into an HBITMAP (which works exactly the same for a JPG).
One word of warning from experience - if you're storing your JPG as a resource, make sure you create a "JPG" resource type and store it as that, don't try and add it as a BITMAP resource or you'll have all sorts of problems trying to load it. I know it sounds obvious but it took me a while to figure out.
You can't, or at least, there isn't a build-in way. Have a look at CxImage, you can probably get that to work, especially when you only need jpg support.
This kind of conversion is complex so i would reccoment using an external libary, take a look at this.
CBitmap bmp;
HANDLE h = ::LoadImage(NULL, _T("c:\\MyImage.jpg"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
bmp.Attach(h);