Loading bitmap in particular way
I have a function that essentially takes a screen shot and saves a pointer to it as a structure. I would like to use the same structure for bitmaps that I load from files.
Code:
typedef struct _BITMAPCAPTURE {
HBITMAP hbm;
LPDWORD pixels;
INT width;
INT height;
} BITMAPCAPTURE;
BOOL CaptureScreen(BITMAPCAPTURE* bmpCapture)
{
BOOL bResult = FALSE;
if(!bmpCapture)
return bResult;
ZeroMemory(bmpCapture, sizeof(BITMAPCAPTURE));
HDC hdcScreen = GetDC(NULL);
HDC hdcCapture = CreateCompatibleDC(NULL);
int nWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN),
nHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
// Bitmap is stored top down as BGRA,BGRA,BGRA when used as
// DWORDs endianess would change it to ARGB.. windows COLORREF is ABGR
LPBYTE lpCapture;
BITMAPINFO bmiCapture = { {
sizeof(BITMAPINFOHEADER), nWidth, -nHeight, 1, 32, BI_RGB, 0, 0, 0, 0, 0,
} };
bmpCapture->hbm = CreateDIBSection(hdcScreen, &bmiCapture,
DIB_RGB_COLORS, (LPVOID *)&lpCapture, NULL, 0);
if(bmpCapture->hbm){
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcCapture, bmpCapture->hbm);
BitBlt(hdcCapture, 0, 0, nWidth, nHeight, hdcScreen, 0, 0, SRCCOPY);
SelectObject(hdcCapture, hbmOld);
bmpCapture->pixels = (LPDWORD)lpCapture;
bmpCapture->width = nWidth;
bmpCapture->height = nHeight;
bResult = TRUE;
}
DeleteDC(hdcCapture);
DeleteDC(hdcScreen);
return bResult;
}
The handle to the bitmap, as well as the width and height are all easy enough to get but I'm unsure of how to get the pixels. Thanks very much.
Re: Loading bitmap in particular way
Quote:
Originally Posted by
IronRhino
I have a function that essentially takes a screen shot and saves a pointer to it as a structure. I would like to use the same structure for bitmaps that I load from files.
What's wrong with CImage::Load?
Re: Loading bitmap in particular way
Code:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
typedef struct _BITMAPCAPTURE {
HBITMAP hbm;
LPDWORD pixels;
INT width;
INT height;
} BITMAPCAPTURE, *PBITMAPCAPTURE;
BOOL LoadBitmapAndGetCaps(LPCTSTR fileName, PBITMAPCAPTURE cap)
{
HBITMAP hbm = (HBITMAP)LoadImage(NULL, fileName, IMAGE_BITMAP,
LR_DEFAULTSIZE, LR_DEFAULTSIZE,
LR_CREATEDIBSECTION | LR_LOADFROMFILE | LR_DEFAULTSIZE);
BITMAP bm = {0};
if (hbm && GetObject((HGDIOBJ)hbm, sizeof(bm), &bm))
{
cap->hbm = hbm;
cap->pixels = (LPDWORD)bm.bmBits;
cap->width = bm.bmWidth;
cap->height = bm.bmHeight;
return TRUE;
}
return FALSE;
}
int _tmain(int argc, TCHAR** argv)
{
if (argc)
{
BITMAPCAPTURE bmc = {0};
BOOL success = LoadBitmapAndGetCaps(argv[1], &bmc);
if (success)
_tprintf(TEXT("Bitmap width = %d, height = %d, bits = 0x%p"),
bmc.width, bmc.height, bmc.pixels);
else
_tprintf(TEXT("Failure with last error %d"), GetLastError());
}
return 0;
}
Code:
E:\Temp\692>692.exe "C:\Users\Igor\Local Settings\Temp\Microsoft Visual Studio 2010 Service Pack 1_10.0.40219\SplashScreen.bmp"
Bitmap width = 64, height = 64, bits = 0x002D0000