I want to load an image (.bmp) file on a Win32 application, but I do not want to use the standard LoadBitmap/LoadImage from Windows API: I want it to load from a buffer that is already in memory. I can easily load a bitmap directly from a file and print it on the screen, but this issue is making me stuck.
What I'm looking for is a function that works like this:
HBITMAP LoadBitmapFromBuffer(char* buffer, int width, int height);
Try CreateBitmap():
HBITMAP LoadBitmapFromBuffer(char *buffer, int width, int height)
{
return CreateBitmap(width, height, 1, 24, buffer);
}
Nevermind, I found my solution! Here's the initializing code:
std::ifstream is;
is.open("Image.bmp", std::ios::binary);
is.seekg (0, std::ios::end);
length = is.tellg();
is.seekg (0, std::ios::beg);
pBuffer = new char [length];
is.read (pBuffer,length);
is.close();
tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)pBuffer;
tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(pBuffer+sizeof(tagBITMAPFILEHEADER));
RGBQUAD rgb = *(RGBQUAD*)(pBuffer+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));
BITMAPINFO bi;
bi.bmiColors[0] = rgb;
bi.bmiHeader = bih;
char* pPixels = (pBuffer+bfh.bfOffBits);
char* ppvBits;
hBitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
SetDIBits(NULL, hBitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);
GetObject(hBitmap, sizeof(BITMAP), &cBitmap);
CreateDIBSection can be a little complicated to use, but one of the things it can do is create a device-independent bitmap and give you a pointer to the buffer for the bitmap bits. Granted, you already have a buffer full of bitmap bits, but at least you could copy the data.
Speculating a bit: CreateDIBSection can also create bitmaps from file objects, and there's probably a way to get Windows to give you a file object representing a chunk of memory, which might trick CreateDIBSection into giving you a bitmap built directly from your buffer.
No, but you can create a new bitmap the size of the current one in memory, and write your memory structure onto it.
You're looking for the CreateBitmap function. Set lpvBits to your data.
Related
I have an HBITMAP handle to a device-dependent bitmap, resulting from this code:
// copy screen to bitmap
HDC hScreen = GetDC(NULL);//don't use hwnd, it doesn't work for non native windows
HDC hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, w, h);
HGDIOBJ old_obj = SelectObject(hDC, hBitmap);
BOOL bRet = BitBlt(hDC, 0, 0, w, h, hScreen, x1, y1, SRCCOPY);
I want to access (read-only) the bits of the bitmap, read some values, do some calculations, then discard it.
According to this answer, I shouldn't use GetDIBits() or GetBitmapBits() since they copy the data, but instead I should use:
BITMAP bitmap;
GetObject(hBitmap, sizeof(bitmap), (LPVOID)&bitmap);
But according to GetObject's documentation:
If hgdiobj is a handle to a bitmap created by calling CreateDIBSection, and the specified buffer is large enough, the GetObject function returns a DIBSECTION structure. In addition, the bmBits member of the BITMAP structure contained within the DIBSECTION will contain a pointer to the bitmap's bit values.
If hgdiobj is a handle to a bitmap created by any other means, GetObject returns only the width, height, and color format information of the bitmap. You can obtain the bitmap's bit values by calling the GetDIBits or GetBitmapBits function.
Which is pointed to in a comment on the above answer:
According to this https://msdn.microsoft.com/en-us/library/dd144904(v=vs.85).aspx if bitmap is not created by CreateDIBSection bitmap.bmBit pointer will be null.
So, is there anyway to access the bits of the bitmap (RGB values), without copying them?
If it helps, the device on which the bitmap is dependent is always a screen, so I guess it's always a 24bit or 32bit bitmap.
In this article, regarding how to save an HBITMAP to a file, there's this section:
// Allocate memory for the BITMAPINFO structure. (This structure
// contains a BITMAPINFOHEADER structure and an array of RGBQUAD
// data structures.)
if (cClrBits < 24) pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1 << cClrBits));
// There is no RGBQUAD array for these formats: 24-bit-per-pixel or 32-bit-per-pixel
else pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));
So, I don't really understand what's happening here, and which is right?
I have function, which creates Gdiplus::Bitmap.
Bitmap *LoadBitmapT(const unsigned char* fileBuffer, size_t length) {
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, length);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem, fileBuffer, length);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem, FALSE, &pstm);
Gdiplus::Bitmap *bitmap = Gdiplus::Bitmap::FromStream(pstm, FALSE);
GlobalUnlock(m_hMem);
pstm->Release();
return bitmap;
}
As you can see, memory leak appears thanks to GlobalAlloc().
When I try to use GlobalFree(m_hmem) it solves my problem and memory leak disappear. But I am drawing obtained bitmap in another function with this code:
Graphics graphics(hdc);
graphics.DrawImage(bitmap, ....);
and when I don't use GlobalFree(), painted image is correct. But when I use mentioned function, than I lose correct image and it is image like blue screen.
Than I try to save pointer of m_hMem and call GlobalFree() after drawing bitmap. So, this is OK. But I need to use rotation on obtained bitmap, so when I call bitMap->RotateFlip(RotateNoneFlipX); than memory leaks appears again. Changing some color of pixel manually in bitmap makes same behaviour.
So, how can I release memory for this image and draw correctly this image. I need it, because I am drawing periodically a lot of images and so this allocates a lot of memory and then my program crash.
Edit
I tried this code:
Bitmap *LoadBitmapT(const unsigned char* fileBuffer, size_t length) {
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, length);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem, fileBuffer, length);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem, FALSE, &pstm);
Gdiplus::Bitmap *bitmap = Gdiplus::Bitmap::FromStream(pstm, FALSE);
GlobalUnlock(m_hMem);
pstm->Release();
GlobalFree(m_hMem);
return NULL;
}
After this when I am looking on task manager, than I see that memory don't increase.
When I give bitmap->RotateFlip(RotateNoneFlipX);after Gdiplus::Bitmap *bitmap = Gdiplus::Bitmap::FromStream(pstm, FALSE); and code is same but only this one line is added, than memory is increasing.
I found answer.
This code deallocates everything correct.
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, length);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem, fileBuffer, length);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem, FALSE, &pstm);
Gdiplus::Bitmap *bitmap = Gdiplus::Bitmap::FromStream(pstm, FALSE);
pstm->Release();
GlobalUnlock(m_hMem);
bitmap->RotateFlip(RotateNoneFlipX);
delete bitmap;
GlobalFree(m_hMem);
Need to call in correct order. So first delete bitmap and then GlobalFree
I have some existing code that uses a CImage which has an alpha channel, and I need to rotate it.
I have found the following suggestion which converts the CImage to a GDI+ Bitmap and then rotates it, and the rotated result ends up back in the CImage.
Bitmap* gdiPlusBitmap=Bitmap::FromHBITMAP(atlBitmap.Detach());
gdiPlusBitmap->RotateFlip(Rotate90FlipNone);
HBITMAP hbmp;
gdiPlusBitmap->GetHBITMAP(Color::White, &hbmp);
atlBitmap.Attach(hbmp);
Apparently it works without actually copying the bitmap bytes, which is great, but the problem is that if you create a Bitmap object from a HBITMAP it throws away the alpha channel.
Apparently to preserve the alpha channel you must instead create the Bitmap using the constructor
Bitmap(
[in] INT width,
[in] INT height,
[in] INT stride,
[in] PixelFormat format,
[in] BYTE *scan0
);
So I'm trying to adapt the above to use this constructor, but the interaction between CImage and Bitmap is a bit confusing. I think I need to create the Bitmap like this
Bitmap* gdiPlusBitmap = new Bitmap(
pCImage->GetWidth(),
pCImage->GetHeight(),
pCImage->GetPitch(),
PixelFormat32bppARGB,
(BYTE *)pCImage->GetBits());
nGDIStatus = gdiPlusBitmap->RotateFlip(Rotate90FlipNone);
but I'm not sure how to make the CImage pick up the changes (so that I end up with the original CImage rotated), or where to delete the Bitmap object.
Does anyone know the correct way to do this, preserving the alpha channel ?
Ideally I'd like to avoid copying the bitmap data, but it's not mandatory.
You can use Gdiplus::Graphics to draw bitmap on CImage.
Note, hard coding PixelFormat32bppARGB can cause problems if image doesn't support alpha channel. I added some basic error check.
CImage image;
if (S_OK != image.Load(L"c:\\test\\test.png"))
{
AfxMessageBox(L"can't open");
return 0;
}
int bpp = image.GetBPP();
//get pixel format:
HBITMAP hbmp = image.Detach();
Gdiplus::Bitmap* bmpTemp = Gdiplus::Bitmap::FromHBITMAP(hbmp, 0);
Gdiplus::PixelFormat pixel_format = bmpTemp->GetPixelFormat();
if (bpp == 32)
pixel_format = PixelFormat32bppARGB;
image.Attach(hbmp);
//rotate:
Gdiplus::Bitmap bmp(image.GetWidth(), image.GetHeight(), image.GetPitch(), pixel_format, static_cast<BYTE*>(image.GetBits()));
bmp.RotateFlip(Gdiplus::Rotate90FlipNone);
//convert back to image:
image.Destroy();
if (image.Create(bmp.GetWidth(), bmp.GetHeight(), 32, CImage::createAlphaChannel))
{
Gdiplus::Bitmap dst(image.GetWidth(), image.GetHeight(), image.GetPitch(), PixelFormat32bppARGB, static_cast<BYTE*>(image.GetBits()));
Gdiplus::Graphics graphics(&dst);
graphics.DrawImage(&bmp, 0, 0);
}
It's easy to produce IWICBitmap from HBITMAP with CreateBitmapFromHBITMAP factory method. But how to get simple GDI bitmap from IWICBitmapSource?
Unfortulately there is no way to convert a IWICBitmap to a HBITMAP, or to get one from a IWICBitmapSource. You could, however, easily create the HBITMAP from a pixel buffer if the format is compatible with what CreateBitmap expects.
Assuming a 32-bit RGBA bitmap:
IWICBitmapSource *bitmap_source = some_func();
UINT width = 0, height = 0;
bitmap_source->GetSize(&width, &height);
std::vector<BYTE> buffer(width * height * 4);
bitmap_source->CopyPixels(0, width * 4, buffer.size(), buffer.data());
HBITMAP bitmap = CreateBitmap(width, height, 1, 32, buffer.data());
There's no guarantee that behind the scenes IWICBitmapSource even uses a GDI bitmap. You can access the raw bits using the IWICBitmap::Lock method, but if you want to use the bitmap with GDI functions I think you will need to create a GDI bitmap and copy the bits to it yourself.
I am using the FreeImage library to store and manipulate bitmap data. Part of my code requires me to take a screenshot of a window (in Windows), and store it as a FBITMAP* (FreeImage's bitmap data structure). The method I have worked out of doing this involves two steps: capture the image, then convert it to a FBITMAP*.
To capture the image, I do something like this:
HWND window; // Assume this is a valid handle to my window
int width; // width of window client area
int height; // height of window client area
HDC windowDC = GetDC(window);
HDC captureDC = CreateCompatibleDC(windowDC);
HBITMAP screenshot = CreateCompatibleBitmap(windowDC, width, height);
SelectObject(captureDC, screenshot);
BitBlt(captureDC, 0, 0, width, height,
captureDC, 0, 0, SRCCOPY|CAPTUREBLT);
ReleaseDC(window, windowDC);
DeleteDC(captureDC);
FreeImage provides a function which returns a raw pointer to the pixel data:
BYTE* FreeImage_GetBits(FBITMAP*)
The FAQ explains that a HBITMAP (WinAPI handle to a bitmap) can be converted to a FBITMAP* using GetDIBits, which takes a source HBITMAP and a destination raw pointer as arguments, and copies the pixel data from one to the other.
The problem with this approach is that I have copied the data twice - once in the BitBlt from the window DC to the HBITMAP selected in the memory DC, and then again from the HBITMAP to the FreeImage memory buffer. I wish to remove this inefficiency and copy the data directly to my raw pointer in the BitBlt operation. For this to work, I need a memory DC which has a HBITMAP selected into it, and where that HBITMAP points to my memory buffer instead of to memory that Windows allocated for it.
How can I achieve this?