透明的图片框闪烁

时间:2017-06-13 15:05:50

标签: c# winforms visual-studio picturebox

所以我正在使用没有游戏引擎编写一个由纯c#制作的植物大战僵尸的项目,这里我有一个图形问题。 我需要在另一个透明的图片框上渲染一个透明的图片框,我不得不定义一个非常透明的新控件,透明度方面的一切都很顺利但这里有一个问题: 闪烁:| 由于RecreateHandle(),我有很多这些;当我更改控件的图像以制作动画时以及何时移动以具有真实透明度时使用的方法。 这是我的代码我想知道是否有人可以提供帮助!

public class TransparentControl : Control
{
    private Image _image;

    public TransparentControl()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint |
             ControlStyles.SupportsTransparentBackColor, true);
        base.BackColor = Color.FromArgb(0, 0, 0, 0);
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
        }
    }

    protected override void OnMove(EventArgs e)
    {
        //RecreateHandle();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (_image != null)
        {
            e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2));
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        //Do not paint background
    }

    //Hack
    public void Redraw()
    {
        //RecreateHandle();
    }
    public Image Image
    {
        get
        {
            return _image;
        }
        set
        {
            _image = value;
            //RecreateHandle();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

减少闪烁的一种方法是创建两个不同的位图。一个要绘制,一个要显示。

Image BackBuffer;
Image BrontBuffer;

private void RotateImages()
{
    lock (this.BackBuffer)
    {
        var temp = this.BackBuffer;
        this.BackBuffer = this.FrontBuffer;
        this.FrontBuffer = temp;
    }
}

所有内容绘制到BackBuffer中,然后显示FrontBuffer。

请注意,您应该声明FrontBuffer具有与BackBuffer相同的宽度/高度值,与您声明BackBuffer的几乎完全相同的位置。

在显示前缓冲区后,(之前也应该)之后立即使用方法RotateImages()

相关问题