views:

557

answers:

1

Hi!

I'm developing and C# app for Windows Mobile. I have a custom control with OnPaint overrided to draw an image that the user moves with the pointer. My own OnPaint method is this:


protected override void OnPaint(PaintEventArgs e)
{
    Graphics gxOff; //Offscreen graphics
    Brush backBrush;

    if (m_bmpOffscreen == null) //Bitmap for doublebuffering
    {
        m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
    }

    gxOff = Graphics.FromImage(m_bmpOffscreen);

    gxOff.Clear(Color.White);

    backBrush = new SolidBrush(Color.White);
    gxOff.FillRectangle(backBrush, this.ClientRectangle);

    //Draw some bitmap
    gxOff.DrawImage(imageToShow, 0, 0, rectImageToShow, GraphicsUnit.Pixel);

    //Draw from the memory bitmap
    e.Graphics.DrawImage(m_bmpOffscreen,  this.Left, this.Top);

    base.OnPaint(e);
}

The imageToShow it's the image.

The rectImageToShow it's initialized on event OnResize in this way:

rectImageToShow = 
   new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);

this.Top and this.Left are the topLeft corner to draw the image inside the custom control.

I think it'll work fine but when I move the image it never clean all the control. I always see a part of the previous drawing.

What I'm doing wrong?

Thank you!

+2  A: 

I think you have not cleared the Control's image buffer. You have only cleared the back buffer. Try this between the 2 DrawImage calls:

e.Graphics.Clear(Color.White);

This should clear any leftover image first.


Alternatively, you can rewrite it so everything is painted on to the back buffer and the back buffer is then paint onto the screen at exactly (0, 0) so any problems will be because of the back buffer drawing logic instead of somewhere in between.

Something like this:

Graphics gxOff; //Offscreen graphics
Brush backBrush;

if (m_bmpOffscreen == null) //Bitmap for doublebuffering
{
    m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
}

// draw back buffer
gxOff = Graphics.FromImage(m_bmpOffscreen);

gxOff.Clear(Color.White);

backBrush = new SolidBrush(Color.White);

gxOff.FillRectangle(backBrush, this.Left, this.Top,
    this.ClientRectangle.Width,
    this.ClientRectangle.Height);

//Draw some bitmap
gxOff.DrawImage(imageToShow, this.Left, this.Top, rectImageToShow, GraphicsUnit.Pixel);

//Draw from the memory bitmap
e.Graphics.DrawImage(m_bmpOffscreen,  0, 0);

base.OnPaint(e);

Not sure if that is correct, but you should get the idea.

chakrit
It works with:e.Graphics.Clear(Color.White);
VansFannel