【问题标题】:C# DrawImage using DoubleBuffered not working使用 DoubleBuffered 的 C# DrawImage 不起作用
【发布时间】:2016-03-31 16:57:40
【问题描述】:

您好,我有一个 c# 用户控件,我覆盖了 OnPaint 方法。

在我的 OnPaint 方法中,我有以下代码:

// Draw the new button. 
protected override void OnPaint(PaintEventArgs e) {
    Color btnColor = this.ButtonColor;
    if (!this.ButtonEnabled) {
        btnColor = Color.LightGray;
    }


    Bitmap GraphicsImage = new Bitmap(24, 24, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    Graphics.FromImage(GraphicsImage).Clear(btnColor);

    Graphics graphics = e.Graphics;
    SolidBrush myBrush = new SolidBrush(btnColor);
    Pen myPen = new Pen(btnColor);
    // Draw the button in the form of a circle
    graphics.DrawEllipse(myPen, 0, 0, 40, 40);
    graphics.FillEllipse(myBrush, new Rectangle(0, 0, 40, 40));



    if (!DesignMode) {
        Image iconImg = null;

        switch (this.ButtonImage) {
            case CircleButtonImage.ArrowDown:
                iconImg = POS.Framework.Utility.Common.GetResourceImage("arrowdown_16.png");
                break;
            case CircleButtonImage.ArrowUp:
                iconImg = POS.Framework.Utility.Common.GetResourceImage("arrowup_16.png");
                break;
            case CircleButtonImage.Cross:
                iconImg = POS.Framework.Utility.Common.GetResourceImage("close_16.png");
                break;
            case CircleButtonImage.Plus:
                iconImg = POS.Framework.Utility.Common.GetResourceImage("plus_16.png");
                break;
        }


        graphics = Graphics.FromImage(GraphicsImage);
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        graphics.DrawImage(iconImg, 5, 5);

        this.CreateGraphics().DrawImageUnscaled(GraphicsImage, new Point(7, 6));

    }


    myBrush.Dispose();
    myPen.Dispose();
}

这很好用,但为了避免闪烁,我添加到我的构造函数中:

 this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);

然后png 图像不再显示。

没有双缓冲:

双缓冲:

有关如何解决此问题的任何线索。我想避免闪烁,但需要渲染图像。

【问题讨论】:

  • 您必须改用e.Graphics

标签: c# winforms onpaint


【解决方案1】:

不要使用CreateGraphics。您需要使用的Graphics 对象在PaintEventArgs 中作为参数传递给OnPaint

另外,你真的应该自己清理一下:

using (var graphics = Graphics.FromImage(GraphicsImage))
{
  graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  graphics.DrawImage(iconImg, 5, 5);
}

GDI+ 对象是非常有限的资源 :) 除此之外,SmoothingMode 对您在这里所做的事情几乎没有用处 - 至少阅读文档而不是仅仅阅读货物真的很有帮助。

【讨论】:

  • 我刚刚评论了这一行:this.CreateGraphics().DrawImageUnscaled(GraphicsImage, new Point(7, 6));但还是同样的问题
  • @VAAA 为什么?这只是阻止图像绘制。只需使用您传递的Graphics 对象。
  • 我把最后一部分改成这样: using (var graphics1 = Graphics.FromImage(GraphicsImage)) { graphics1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; graphics1.DrawImage(iconImg, 5, 5); graphics1.DrawImageUnscaled(GraphicsImage, new Point(7, 6));但是 png 图像没有被渲染。顺便说一句,非常感谢您的帮助。
  • @VAAA 您需要使用传递给您的OnPaint 方法的Graphics 对象,而不是您在某处找到的随机Graphics 对象。
  • 我只是用OnPaint下的孔代码更新问题,你会看到graphics = e.Graphics。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多