显示图像的自定义控件:将图像移动到内部

发布于 2024-07-14 18:24:59 字数 1257 浏览 10 评论 0原文

我正在为 Windows Mobile 开发 C# 应用程序。 我有一个自定义控件,其中 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 是在自定义内部绘制图像的左上角控制。

我认为它会工作得很好,但是当我移动图像时它永远不会清除所有控件。 我总是看到上一张图的一部分。

我做错了什么?

谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2024-07-21 18:24:59

我认为你还没有清除控件的图像缓冲区。 您仅清除了后台缓冲区。 在 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);

不确定这是否正确,但你应该明白了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文