Sign in to follow this  
Toolmaker

MFC/Win32: Loading images as 24-bit bitmaps...

Recommended Posts

Toolmaker    967
I'm currently looking into a way to load different image types into memory as 24-bit bitmaps that allow me to access the RAW pixel data without too much hassle. I currently use the IPicture interface, but the damn thing refuses to return the final pixel data as 24-bit. I have no use for any of the pixel data if it's not in 24-bit data, because I need to come up with all sorts of constructs to handle the different pixel formats. What would be the best approach to this? Draw the returned image to an offscreen buffer and use that as buffer data? Or is there library(Non (L)GPL) that could handle this for me? Toolmaker [Edited by - Toolmaker on December 2, 2008 5:04:20 AM]

Share this post


Link to post
Share on other sites
Toolmaker    967
Alright, I've worked out a little code, that doesn't work quite like I want it to work... I'm using GetDIBits to get an device independant bitmap back in 24-bit RGB format.

However, it returns 0 scanlines and pData is still 0. Any ideas what's going wrong here?


HBITMAP hBitmap;
DWORD size = m_xBitmapPixels * m_yBitmapPixels * 3;
BYTE *pData = new BYTE[size]; // Returned bitmap is 24-bit!
ZeroMemory(pData, size * sizeof(BYTE));

if (SUCCEEDED(pictureFile.m_IPicture->get_Handle((OLE_HANDLE*)&hBitmap)))
{
CBitmap bmp;
CDC *pDC = new CDC();
if (pDC->CreateCompatibleDC(NULL) && bmp.Attach(hBitmap))
{
pDC->SelectObject(&bmp);
BITMAPINFO bmpInfo;

ZeroMemory(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = m_xBitmapPixels;
bmpInfo.bmiHeader.biHeight = m_yBitmapPixels;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;

int scanLines = GetDIBits(pDC->m_hDC, hBitmap, 0, m_yBitmapPixels, (LPVOID) pData, &bmpInfo, DIB_RGB_COLORS);
scanLines = 0; // Debug stuff
}
delete pDC;
}



Toolmaker

Share this post


Link to post
Share on other sites
Toolmaker    967
The returned size/bitcount/etc. are all correct. However, even if I use standard Win32 code for this, it still returns 0 scanlines.


HDC hdc = CreateCompatibleDC(NULL);

if (hdc != NULL)
{
BITMAPINFO bmpInfo;
ZeroMemory(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = m_xBitmapPixels;
bmpInfo.bmiHeader.biHeight = m_yBitmapPixels;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;

HGDIOBJ obj = SelectObject(hdc, hBitmap);
int scanLines = GetDIBits(hdc, hBitmap, 0, m_yBitmapPixels, (LPVOID) pData, &bmpInfo, DIB_RGB_COLORS);
scanLines = 0; // Debug stuff
}



Kinda confused right now

Share this post


Link to post
Share on other sites
Evil Steve    2017
What does the following give?


HDC hdc = CreateCompatibleDC(NULL);
if (hdc != NULL)
{
HBITMAP hBitmap = CreateBitmap(m_xBitmapPixels, m_yBitmapPixels, 1, 24, NULL);
if(hBitmap != NULL)
{
BITMAPINFO bmpInfo;
ZeroMemory(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = m_xBitmapPixels;
bmpInfo.bmiHeader.biHeight = m_yBitmapPixels;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;

HGDIOBJ obj = SelectObject(hdc, hBitmap);
int scanLines = GetDIBits(hdc, hBitmap, 0, m_yBitmapPixels, (LPVOID) pData, &bmpInfo, DIB_RGB_COLORS);
scanLines = 0; // Debug stuff
}
}


Share this post


Link to post
Share on other sites
Evil Steve    2017
Ah, I see - from the Docs:
Quote:
hbmp [in] Handle to the bitmap. This must be a compatible bitmap (DDB).

The bitmap in the code I posted and the bitmap from IPicture are both non-compatible bitmaps. You'd need to use CreateCompatibleBitmap() to create a compatible bitmap for GetDIBits() to work. Or use a DIB section, which might be easier.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this