使用透明bg添加GIF图片

时间:2012-08-13 10:03:05

标签: c# picturebox

我使用this control但它不支持GIF文件格式,因为图片不像PictureBox那样移动。

1 个答案:

答案 0 :(得分:0)

尝试在OnPaint事件中使用Bitmap.MakeTransparentImageAnimator.UpdateFrames来简化PictureBox

private Image animGif;// gif animation file

private void Form1_Load(object sender, EventArgs e)
{
    animGif = new Bitmap("file.png");
    ImageAnimator.Animate(animGif, Animate);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    ImageAnimator.UpdateFrames();
    var img = new Bitmap(animGif, animGif.Width, animGif.Height);
    img.MakeTransparent(Color.White);
    e.Graphics.Clear(pictureBox1.BackColor);
    e.Graphics.DrawImage(img, new Point(0, 0));
}

private void Animate(object sender, EventArgs e)
{
    pictureBox1.Invalidate();
}