显示图像的自定义控件:移动图像

问题描述:

我正在开发和C#应用程序Windows Mobile。我有一个自定义控件,用OnPaint覆盖来绘制用户随指针移动的图像。我自己的OnPaint方法是这样的:显示图像的自定义控件:移动图像

 

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); 
} 
 

imageToShow它的图像。

rectImageToShow它以这种方式对事件onResize受到初始化:

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

this.Topthis.Left是的左上角绘制自定义控件内的图像。

我认为它可以正常工作,但是当我移动图像时,它永远不会清除所有的控件。我总是看到以前绘图的一部分。

我在做什么错了?

谢谢!

我想你还没有清除控件的图像缓冲区。您只清除了后台缓冲区。在2个DrawImage调用之间试试:

e.Graphics.Clear(Color.White); 

这应该先清除任何剩余的图像。


或者,你可以把它改写所以一切都画上到后台缓冲区,然后回缓冲区绘制到屏幕上正好(0,0),所以任何问题,是因为后台缓冲区绘图逻辑而不是介于两者之间。

事情是这样的:

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); 

不知道这是正确的,但你应该明白我的意思。

+0

它适用于: e.Graphics.Clear(Color.White); – VansFannel 2009-03-01 09:17:28