LoadImage Function isn't Working With ImageMagick Created Bitmap Images - c++

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.

Related

Cannot get an image handle from a bitmap within the resources when using "LoadImageA()" and cannot understand why

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.

OpenCV imwrite not writing image

I was trying to write an image to a folder using OpenCV imwrite function.
The code compiles and runs successfully but the image is not saving in the folder/path.I am getting output from 'imshow' and my image is in CV_8UC1 format.
find the code below
Mat reflection = function which computes image
imshow("output image", reflection);
imwrite("E:/New folder/img.bmp", reflection);
so I checked current folder writing and modified code like this
bool check = imwrite("./img.bmp", reflection);
this 'bool check' status is 'false' and not writing the image.
I've also checked folder permission with guidelines from microsoft help my "E/New Folder/" is permitted to write. still, the image is not saving.
I am okay with any image format .jpg, .png and .bmp.
I am using windows 7, OpenCV 3.0, visual studio 2017.
Please help me and thanks for reading it
Opencv doesn't seem to support saving BMP files check the imwrite docs. Changing the filename to img.png should work. Also using ./ in windows is not valid, this is used in unix systems to represent the current working directory see Windows current directory.
Updating it to
bool check = imwrite(".\img.png", reflection);
or
bool check = imwrite("img.png", reflection);
Should work
OpenCV do support bmp, just use as follows.
bool check = imwrite("img.bmp", reflection);

Loading an image from a resource embedded in a dll

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.

How can I load a jpg file into CBitmap (visual c++ 6.0)?

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);

Loading JPEG file from resources in MFC C++ application

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".