Creating a 24-Bit Per Pixel Subregion of a 24-Bit Per Pixel Bitmap

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Original Bitmap

Subregion of the original bitmap

Environment: VC6

This article shows you how to create a portion of a 24-bit/pixel bitmap at runtime. In order to do this, you must add a 24-bit/pixel bitmap to your resources in VC++, decide what portion of the bitmap you wish (specified by the x-y coordinates of the upper left corner and the horizontal/vertical size of the portion), and what .bmp file you want the new portion of the bitmap to be store. The .bmp file created can be used at a later time, but in addition the bitmap of the subregion can be retrieved and used, for example, in your OnDraw function.

In the following code sample, we create a portion of a huge bitmap. The subregion is 416 by 224 pixels, but the original bitmap is well over 100 times this size.


CBitmapSubRegion bsr;
BOOL bSuccess;
// IDB_BITMAP_SCHUMACHERCAR is a 24-bit/pixel
// resource ID added to VC++

bSuccess = bsr.SetResourceID(IDB_BITMAP_SCHUMACHERCAR);
if (bSuccess)
{
bSuccess = bsr.SetFileNameOfResourceBitmap(
CString(“SchumacherCar.bmp”));
if (bSuccess)
{
// Want to create a subregion which starts at
// (x,y) of (259,317) and is 416-pixels wide by
// 224-pixels tall

bSuccess = bsr.SetSubRegionOfBitmap(259,317,416,224);
if (bSuccess)
{
//Set where to store the new bitmap

bSuccess = bsr.SetFileNameForNewBitmap(
CString(“SuhumacherCarSubRegion.bmp”));
if (bSuccess)
{
bSuccess = bsr.CreateBitmapOfSubRegion();
if (bSuccess)
{
m_pBitmapSubRegion =
bsr.RetrieveBitmapOfSubRegion();
}
}
}
}
}

Downloads

Download source – 5 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read