Bitmap: how fill with image before call to BitBlt()?

G

Guest

Hey. I've got a color photo in a simple unsigned charRGB array. The photo
does not exist on disk. I want to display the photo in my window.

The application I am working in (not my choice) demands that I draw into the
display by first creating a Bitmap, and then drawing into a DisplayContext
using BitBlt(). The pseudocode looks something like:

// Create an empty bitmap:
HDC hScreen = GetDC (0);
pHandle = CreateCompatibleBitmap (hScreen, width, height);
// Here is the problem: How do I copy my photo into the bitmap
// at this point??
// .... TBS .... magic here ....
// Now write the bitmap into the display:
HDC hdcMemory = CreateCompatibleDC ((HDC)pSystemContext);
hOldObj = SelectObject (hdcMemory, pHandle);
BitBlt ((HDC)pSystemContext, ....(HDC)hdcMemory, ..., SRCCOPY);

My question is: How do I transfer the pixels from my photo in memory
(unsigned char array) into the Bitmap? I understand that the bitmap
type - by design - is very opaque and I dont expect to have direct access to
the innards. But surely there must be some function call that will take an
RGB array of pixels and put it into the bitmap?

Thanks in advance for any help,
neal
 
J

Jonathan Wilson

My question is: How do I transfer the pixels from my photo in memory
(unsigned char array) into the Bitmap? I understand that the bitmap
type - by design - is very opaque and I dont expect to have direct access to
the innards. But surely there must be some function call that will take an
RGB array of pixels and put it into the bitmap?
Try SetDIBits
 
J

Jonathan Wilson

My question is: How do I transfer the pixels from my photo in memory
(unsigned char array) into the Bitmap? I understand that the bitmap
type - by design - is very opaque and I dont expect to have direct access to
the innards. But surely there must be some function call that will take an
RGB array of pixels and put it into the bitmap?
Or you could use CreateDIBSection to create the HBITMAP, that might work too.
 
G

Guest

I got this working, finally.

For the record, here is the code that does the job: note I chose to use the
function SetDIBitsToDevice() although StretchDIBits() will also work.


=========================

// createa dummy image
unsigned char img[3*100*100];

for ( int i =0; i< (3*100*100); i++ ) img = 0;

for ( int r=0; r<100; r++ ) {
for ( int c=0; c<100; c++ ) {

img[ 3*(r *100 + c) ] = r*2; // unsinged char
img[ 3*(r*100 + c) + 1] = 255;
img[ 3*(r*100 + c) + 2] = 255;

}
}

long width = 100;
long height = 100;

HWND hwnd;
hwnd = (HWND)(frame->getSystemWindow ());
HDC hdc = GetDC (hwnd);

// Allocate enough memory for the BITMAPINFOHEADER and 256 RGBQUAD
palette entries
// NOTE: the pallet bytes are ONLY NEEDED for color LUT images ...
not needed here
LPBITMAPINFO lpbi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)
+ (1 * sizeof(RGBQUAD))];

lpbi->bmiColors[1].rgbBlue = 0; // NOT NEEDED
lpbi->bmiColors[1].rgbGreen = 0;
lpbi->bmiColors[1].rgbRed = 0;

// These are all the members of the bitmap header struct
lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); // bytes
lpbi->bmiHeader.biWidth = 100; // pixels units
lpbi->bmiHeader.biHeight = -100; // negative == top down; pos =
origin LLeft
lpbi->bmiHeader.biPlanes = 1; // must be 1
lpbi->bmiHeader.biBitCount = 24; // can be 32 for 4-byte pixels
(Upper byte ignored)
lpbi->bmiHeader.biCompression = BI_RGB; // BI_RGB means uncompressed
lpbi->bmiHeader.biSizeImage = 0; // size of img in bytes; 0 okay
for BI_RGB
lpbi->bmiHeader.biXPelsPerMeter = 0;
lpbi->bmiHeader.biYPelsPerMeter = 0;
lpbi->bmiHeader.biClrUsed = 0; // LUTs only
lpbi->bmiHeader.biClrImportant = 0; // LUTs only

// Draw the image into the CRT device
::SetDIBitsToDevice(
hdc, // handle to DC
0,0 , // x-y-coord of destination upper-left corner
100, 100, // width-height of source rectangle
0,0, // x-y-coord of source upper-left corner
0,//uStartScan,// first scan line in array
100, // number of scan lines ... usu same as height above
img, // array of DIB bits
lpbi, // bitmap information
DIB_RGB_COLORS); // RGB vs. palette indexes ... RGB means raw
RGB pixels;
// alternative is a color LUT pallet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top