locked
CreatePatternBrush / Ellipse not working on HTC Tytn II RRS feed

  • Question

  • 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 ).
    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 it on my real device (HTC Tytn II) all I get is a white circle not that circle filled with the bitmap. I checked the return codes of every line on the device - everything reports no error.
    Anybody an idea what's going on here?

    Thanks
    Maik
    • Edited by MSWiege Friday, August 14, 2009 4:24 PM new title as it seem to refer to the Tytn II only
    Thursday, August 13, 2009 9:47 AM

All replies

  • I now tried this example on some different devices:
    - All emulators work fine
    - HTC Wizard 200 (WM 5) works
    - HTC Diamond (WM 6.1) works
    only
    - HTC Tytn II (WM 6.1, ROM-Version 3.28.407.0 GER) doesn't work (even after a fresh hard reset)

    It seems that the Tytn II has a bug in the PatternBrush. The bursh simply doesn't paint on the DC - the region is drawn white. I build a simple example project, that only paint a bitmap red and creats a PetternBrush from the red Bitmap. Drawing with that brush on the screen should result in red circle. As mentioned above this is in most cases true, only on my HTC Tytn II the circle is filled white.

            [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 Form1_Paint(object sender, PaintEventArgs e)
            {
                Bitmap bmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
                Graphics g = Graphics.FromImage(bmp);
                g.Clear(Color.Red);
                Rectangle rect = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
                rect.Inflate(this.ClientSize.Width / -4, this.ClientSize.Height / -4);
    
                DrawCircleOfBitmap(e.Graphics, bmp, rect);
    
                g.Dispose();
                bmp.Dispose();
            }
            private void DrawCircleOfBitmap(Graphics g, Bitmap bmp, Rectangle rect)
            {
                IntPtr hBitmap = bmp.GetHbitmap();
                IntPtr hBrush = CreatePatternBrush(hBitmap);
                IntPtr hDC = g.GetHdc();
                IntPtr hOldBrush = SelectObject(hDC, hBrush);
                Ellipse(hDC, rect.Left, rect.Top, rect.Right, rect.Bottom);
                // Release of native GDI objects
                SelectObject(hDC, hOldBrush);
                DeleteObject(hBrush);
                g.ReleaseHdc(hDC);
                DeleteObject(hBitmap);
            }


    Does anybody know of this issue of the Tytn II? Or does anybody else can test it on his device so I can see if this is a general problem and I shouldn't use this code in my software or if it is just some kind of fault in my specific device. You can download the test project including sources and binary here:
    http://www.maik-wiege.de/test.zip

    I hope someone could tell me his experience with his device and the PatternBrush. Of course best would be if anybody knows a workaround for this, because I can't even tell from the code that something went wrong because none of the GDI functions returns an error on the device.
    • Edited by MSWiege Monday, August 17, 2009 10:43 AM
    Friday, August 14, 2009 4:24 PM
  • Hi,,

    The code runs successfully on my HTC Diamond and my HTC Cruise. It also works on my HTC Artemis and HTC Prophet.

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Edited by Paul Diston Friday, August 14, 2009 6:49 PM
    • Marked as answer by ZHE ZHAO Wednesday, August 19, 2009 1:32 AM
    • Unmarked as answer by MSWiege Wednesday, August 19, 2009 7:15 AM
    Friday, August 14, 2009 4:33 PM
  • Thank you very much Paul for your test results!
    Anybody else especially with a HTC Tytn II running WM 6.1 willing to test it?
    Friday, August 14, 2009 6:45 PM