INFO: Rotating a Bitmap by 90 DegreesID: Q77127
|
There are no Windows functions that directly rotate bitmaps. All techniques for rotating bitmaps in Windows involve copying the rows from a source bitmap into the columns of a destination bitmap. The following contains code for rotating a bitmap using GetPixel() and SetPixel(), and contains an outline of code for rotating device independent bitmaps (DIB).
A device dependent bitmap (DDB) can be rotated using the GetPixel() and SetPixel() functions. To rotate the bitmap, use the following code:
HBITMAP Rotate90(HDC hDC, HBITMAP hSourceBitmap)
{
HBITMAP hOldSourceBitmap, hOldDestBitmap, hDestBitmap;
HDC hMemSrc, hMemDest;
int height, width;
int i, j;
BITMAP iSrcBitmap;
// Step 1: Create a memory DC for the source and destination bitmaps
// compatible with the device used.
hMemSrc = CreateCompatibleDC(hDC);
hMemDest= CreateCompatibleDC(hDC);
// Step 2: Get the height and width of the source bitmap.
GetObject(hSourceBitmap, sizeof(BITMAP), (LPSTR)&SrcBitmap);
width = SrcBitmap.bmWidth;
height = SrcBitmap.bmHeight;
// Step 3: Select the source bitmap into the source DC. Create a
// destination bitmap, and select it into the destination DC.
hOldSourceBitmap = SelectObject(hMemSrc, hSourceBitmap);
hDestBitmap = CreateBitmap(height, width, SrcBitmap.bmPlanes,
SrcBitmap.bmBitsPixel, NULL);
if (!hDestBitmap)
return(hDestBitmap);
hOldDestBitmap = SelectObject(hMemDest, hDestBitmap);
// Step 4: Copy the pixels from the source to the destination.
for (i = 0; i < width; ++i)
for (j = 0; j < height; ++j)
SetPixel(hMemDest, j, width - 1 - i,
GetPixel(hMemSrc, i, j));
// Step 5: Destroy the DCs.
SelectObject(hMemSrc, hOldSourceBitmap);
SelectObject(hMemDest, hOldDestBitmap);
DeleteDC(hMemDest);
DeleteDC(hMemSrc);
// Step 6: Return the rotated bitmap.
return(hDestBitmap);
}
If the bitmap is larger, using GetPixel() and SetPixel() may be too slow. If this is the case, there are two options:
Additional query words: 3.00 3.10 4.00 win16sdk
Keywords : kbSDKWin32 kbWinOS310 kbWinOS95 kbDSupport kbWinOS300
Version : WINDOWS:3.0,3.1,95; winnt:
Platform : WINDOWS winnt
Issue type : kbinfo
Last Reviewed: July 13, 1999