Article ID: 186589 - Last Review: February 22, 2007 - Revision: 2.4 How To Display a Bitmap into a Rotated or Non-rectangular AreaThis article was previously published under Q186589 On This PageSUMMARY
While the Windows GDI provides numerous functions that allow developers to
display bitmaps into rectangular areas, it doesn't provide a way to display
into areas defined by an arbitrary set of vertices. The sample code in this
article demonstrates a simple way to map a bitmap from a rectangular area
into an area defined by four vertices. You can use this technique to
rotate, invert, stretch, or twist a bitmap.
NOTE: For simplicity, the code is written to use GetPixel() and SetPixel() to retrieve and set pixel values in the referenced display contexts. You can achieve much greater performance by rewriting the code to use DIB sections and directly manipulating the bits of the surface. MORE INFORMATIONSample Code
cr = GetPixel(hdcSrc, (x1 >> SHIFTS), (y1 >> SHIFTS));
SetPixel(hdcDst, (ul.x >> SHIFTS), (ul.y >> SHIFTS), cr);
} else { // No
// Quarter the source and the destination, and then recurse.
tm.x = (ul.x + ur.x) >> 1;
tm.y = (ul.y + ur.y) >> 1;
bm.x = (ll.x + lr.x) >> 1;
bm.y = (ll.y + lr.y) >> 1;
lm.x = (ul.x + ll.x) >> 1;
lm.y = (ul.y + ll.y) >> 1;
rm.x = (ur.x + lr.x) >> 1;
rm.y = (ur.y + lr.y) >> 1;
m.x = (tm.x + bm.x) >> 1;
m.y = (tm.y + bm.y) >> 1;
mx = (x1 + x2) >> 1;
my = (y1 + y2) >> 1;
CopySourceToDest(hdcDst, ul, tm, m, lm, hdcSrc, x1, y1,
mx, my);
CopySourceToDest(hdcDst, tm, ur, rm, m, hdcSrc, mx, y1,
x2, my);
CopySourceToDest(hdcDst, m, rm, lr, bm, hdcSrc, mx, my,
x2, y2);
CopySourceToDest(hdcDst, lm, m, bm, ll, hdcSrc, x1, my,
mx, y2);
};
};
lr.x <<= SHIFTS;
lr.y <<= SHIFTS;
ll.x <<= SHIFTS;
ll.y <<= SHIFTS;
x1 <<= SHIFTS;
y1 <<= SHIFTS;
x2 <<= SHIFTS;
y2 <<= SHIFTS;
CopySourceToDest(hdcDst, ul, ur, lr, ll,
hdcSrc, x1, y1, x2, y2);
} | Article Translations
|
Back to the top
