LoadImage() with QRCode bitmap failing unless file is opened/saved with MS Paint first

柔情痞子 提交于 2020-05-27 12:13:32

问题


I am trying to read a bmp file using below function in c++

HANDLE hBmp = LoadImage(0, L"C:\\Users\\abhinay\\Desktop\\Sample.bmp", IMAGE_BITMAP, 0, 0,    LR_CREATEDIBSECTION | LR_LOADFROMFILE);

In my scenario the sample.bmp is a QRCode which is generated by a 3rd part QRCode library. When i try to read QRCode generated with above "LoadImage" function i get hBmp as "NULL".

I opened QRCode image "sample.bmp" with MS paint and saved it as .bmp in 24-bit Bitmap and now i am able to load the file using the same "LoadImage" function above.

Can you please help why the bmp file was not loaded in the first case and how can i make the the generated QRCode image to be loaded properly loaded using "LoadImage" function without the need of converting into 24-bit Bitmap image using MS Paint. Also let me know if its easy to print a .jpg or .png image instead of a .bmp file.

Thanks Abhinay

Edit I have tried using "GetLastError()" as mentioned below

HANDLE hBmp = LoadImage(NULL, bmpfile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

if (hBmp == NULL)
{
    DWORD dw = GetLastError();
    if (dw == NULL)
    {
        MessageBoxA(NULL, "get last error is null", "ABHINAY", MB_OK | MB_TOPMOST);
    }
    else
    {
        MessageBoxA(NULL, "get last error is not null", "ABHINAY", MB_OK | MB_TOPMOST);
    }

}

And i get the error message "get last error is null".


回答1:


Can you please help why the bmp file was not loaded in the first case?

There are lots of variations of BMP format. It's quite possible that the original image was in a variant that LoadImage cannot directly convert to a DIB. So you converted it in Paint to a BMP variant that it could open.

In particular, there are variants where raw PNG of JPG data can be packed into a BMP container. This is typically used to pass the compressed image data directly to a printer that can decompress itself. (In my experience, only a few printers actually support this.) I don't think the GDI API can actually do much else with BMPs of this type. Paint, on the other hand, has codecs for PNG and JPG, so I'd expect its repertoire might include those formats, even when they're packed in a BMP header.

how can i make the the generated QRCode image to be loaded properly loaded using "LoadImage" function without the need of converting into 24-bit Bitmap image using MS Paint.

I don't think you'll be able to do it with LoadImage without converting the file. Modern versions of Windows have other APIs that can load BMPs (and PNG and JPG), so you might try one of these.

  • GDI+ (probably the simplest)
  • WIC
  • OLE (probably the most complex, especially if you're not used to COM)

Also let me know if its easy to print a .jpg or .png image instead of a .bmp file.

If you use one of the APIs I listed to load the image, printing it should be pretty straightforward.



来源:https://stackoverflow.com/questions/26159489/loadimage-with-qrcode-bitmap-failing-unless-file-is-opened-saved-with-ms-paint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!