首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# DrawImage使用DoubleBuffered不工作

C# DrawImage使用DoubleBuffered不工作
EN

Stack Overflow用户
提问于 2016-03-31 16:57:40
回答 1查看 299关注 0票数 0

嗨,我有一个c#用户控件,我重写了OnPaint方法。

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

代码语言:javascript
复制
// 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();
}

这很好,但是为了避免闪烁,我添加了构造函数:

代码语言:javascript
复制
 this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);

那么png图像就不再显示了。

无双缓冲的:

有双重缓冲的:

有什么好办法解决的吗。我想避免闪烁,但需要图像被渲染。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-31 17:01:13

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

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

代码语言:javascript
复制
using (var graphics = Graphics.FromImage(GraphicsImage))
{
  graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  graphics.DrawImage(iconImg, 5, 5);
}

GDI+对象是一个非常有限的资源:)除此之外,SmoothingMode对于您在这里所做的工作几乎没有什么用处--至少阅读文档而不仅仅是对货物的截取非常有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36339399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档