Archived

This topic is now archived and is closed to further replies.

MajorShredd

Converting A Custom Image File To Device Context

Recommended Posts

Hello, I have a file type that has RGB values stored in it just like a bitmap (along with other things though) and I need to extract the RGB values from my file and store them in a Windows Device Context. The problem I am having is making the conversion. I tried a really bad hack at first, like this:
//this is a part of my class that makes the conversion
//  *rasterImageData is a dynamic 1D array of a struct that
//  hold the RGB data in it, and rhDimensions is a struct
//  that contains file header info pertinent only to the
//  image.  All vars in rhDimensions are of type "BYTE"
//  in Win32.

HDC RasterImage::ConvertToDC(HDC hOutputDC){

	COLORREF crPixel;
	int c=0;

	hOutputDC  = CreateCompatibleDC(NULL);
	HBITMAP hb = CreateCompatibleBitmap(hOutputDC,rhDimensions.nWidth,rhDimensions.nHeight);

	for(int y=0; y < rhDimensions.nHeight; y++){
		for(int x=0; x < rhDimensions.nWidth; x++){

			crPixel = RGB(rasterImageData[c].nRed, rasterImageData[c].nGreen, rasterImageData[c].nBlue);

			SetPixel(hOutputDC,x,y,crPixel);
			c++;
		}
	}

	SelectObject(hOutputDC,hb);
	return hOutputDC;
}


//just to test out my code, I tried to blit just the image
//  on a test window, this is the paint handler for it

		case WM_PAINT:{
			hdc = BeginPaint(hwnd,&ps);
			BitBlt(hdc,0,0,5,5,g_hTestDC,0,0,SRCCOPY);
			EndPaint(hwnd,&ps);
			return 0;} break;
  
My output is just all black pixels, which is not what I have stored in my file. Is there a "right" way to do this, if any? Many thanks in advance, -~- MajorShredd -~- EDIT: source formatting was confusing/half missing [edited by - MajorShredd on October 6, 2003 1:51:20 PM] [edited by - MajorShredd on October 6, 2003 1:56:12 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
using CreateCompatibleBitmap against a memory DC will result in a bitmap that''s compatible with the bitmap that''s currently selected into that memory DC, which in the case of a newly created memory DC will be a monochrome bitmap.

so, try using CreateCompatibleBitmap with a DC that has the characteristics that you want for the bitmap. for instance, if you want to create a bitmap that''s compatible with your current display settings, you would use a DC "gotten" via GetDC(NULL) (dont'' forget to Release it later, also using NULL for the window handle).

also, there are GDI functions which help you set bitmap pixel bits so that you don''t have to use SetPixel. look into SetDIBits and SetDIBitsToDevice. for bitmap formats that don''t use a palette (anything above 16bpp), all you need is a pointer to your pixel data and a pointer to a bitmap info struct, which is pretty simple to construct. for bitmap formats below 16bbp you can use SetDIBColorTable for the same purpose.

Share this post


Link to post
Share on other sites