Introduction
This article explains how you can build a bitmap from an array of bytes.This array of bytes is nothing but the byte information of some bitmap which may be obtained in various ways; for example from a camera board whose buffer contains the image data in the form of an array of bytes and you want to build the actual image from this data. For this article we will be obtaining the array of bytes from a known bitmap and use this array to build an initially empty bitmap resource.
Getting Started
- Use MFC AppWizard to generate an SDI application.
- Use the resource editor to build two bitmaps, both of the same dimensions,one of them will actually have some image in it while the other will be a blank image.
Adding Code
Modify your
OnDraw()
to look like this :
void CBmpTryView::OnDraw(CDC* pDC)
{
CBmpTryDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CDC memdcX,memdcY;
memdcX.CreateCompatibleDC(pDC);
memdcY.CreateCompatibleDC(pDC);
BITMAP bmpX,bmpY;
CBitmap mybmp,bmpClone;
bmpClone.LoadBitmap(IDB_BITMAP2);
DWORD dwValue,dwValue2;
if(TRUE == mybmp.LoadBitmap(IDB_BITMAP1))
{
mybmp.GetBitmap(&bmpX);
BYTE* bmpBuffer=(BYTE*)GlobalAlloc(GPTR,
bmpX.bmWidthBytes*bmpX.bmHeight);
dwValue=mybmp.GetBitmapBits(bmpX.bmWidthBytes*bmpX.bmHeight,
bmpBuffer);
dwValue2 = bmpClone.SetBitmapBits(bmpX.bmWidthBytes*bmpX.bmHeight,
bmpBuffer);
bmpClone.GetBitmap(&bmpY);
memdcX.SelectObject(mybmp);
memdcY.SelectObject(bmpClone);
pDC->BitBlt(10,10,bmpX.bmWidthBytes,bmpX.bmHeight ,&memdcX,
0,0,SRCCOPY);
pDC->BitBlt(10,40,bmpX.bmWidthBytes,bmpX.bmHeight ,&memdcY,
0,0,SRCCOPY);
GlobalFree((HGLOBAL)bmpBuffer);
}
}
That's it
The code is by and large self explanatory,if you still need any explanation I will be glad to write back. Also please write if theres any better way to do this.