To draw a circular part of a bitmap on the screen, I use a PatternBrush created from the bitmap to fill an ellipse. I use P/Invoke to the native functions because there seems to be a bug in CF2.0 if you use the managed functions (see here for more details: http://social.msdn.microsoft.com/forums/en-US/netfxcompact/thread/e831ea2f-039a-4b92-adb6-941954bee060/).
Here is the code I use:
[DllImport("coredll.dll")]
private extern static int Ellipse(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
[DllImport("coredll.dll")]
private extern static IntPtr CreatePatternBrush(IntPtr hImage);
[DllImport("coredll.dll")]
private extern static IntPtr CreatePen(int fnPenStyle, int nWidth, uint crColor);
[DllImport("coredll.dll")]
private extern static IntPtr SelectObject(IntPtr hDC, IntPtr hBrush);
[DllImport("coredll.dll")]
private extern static bool DeleteObject(IntPtr hBrush);
private void DrawCircleOfBitmap(Graphics g, Bitmap bmp, Rectangle rect)
{
IntPtr hBitmap = bmp.GetHbitmap(); // get HBitmap
IntPtr hBrush = CreatePatternBrush(hBitmap); // create the PatternBrush
IntPtr hPen = CreatePen(5, 1, 0); // empty Pen (PS_NULL = 5)
IntPtr hDC = g.GetHdc(); // get HDC
IntPtr hOldBrush = SelectObject(hDC, hBrush); // select Brush into context
IntPtr hOldPen = SelectObject(hDC, hPen); // select Pen into context
Ellipse(hDC, rect.Left, rect.Top, rect.Right, rect.Bottom);
// Release of native GDI objects
SelectObject(hDC, hOldBrush);
SelectObject(hDC, hOldPen);
DeleteObject(hBrush);
DeleteObject(hPen);
g.ReleaseHdc(hDC);
DeleteObject(hBitmap);
}
This works perfect on every emulator (WM6, WM6.1.4, WM6.5), but if I use exactly the same on my real device (HTC Tytn II) all I get is a white circle. The circle is not filled with the bitmap. I checked the return codes of every line on the device - everything reports no error. Replacing CreatePatternBrush with CreateSolidBrush works, than it fills the circle with a color. Anybody an idea why the pattern brush is not working?
Thanks Maik