繁体   English   中英

在计时器滴答事件中加载winforms后的图像闪烁

[英]Flickering image after loading in winforms at timer tick event

我的问题如下:我有一个计时器,我想每1毫秒加载一次图像,并以win形式将其显示在图片框中。第一张图像工作得很好,但是从某些时候开始,图像开始很难加载。 你能告诉我如何解决这个问题吗?

谢谢你的时间:)

   private string path = "";
    private int index = 0;
    private void fileSourceToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            string fullPath = ofd.FileName;
            string fileName = ofd.SafeFileName;

            path = fullPath.Replace(fileName, "");

            MessageBox.Show("Path for file source has been selected");
        }
    }

    private const string IMAGENAME = "TestImage";
    Bitmap b;
    private void timer_Tick(object sender, EventArgs e)
    {
        try
        {
            string currentImage = IMAGENAME + index.ToString() + ".bmp";
            index++;
            b = new Bitmap(path + "\\" + currentImage);

            pb.SizeMode = PictureBoxSizeMode.StretchImage;
            pb.Image = b;
        }
        catch { };

    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer.Stop();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            string currentImage = IMAGENAME + index.ToString() + ".bmp";
            index += 1;
            Bitmap b = new Bitmap(path + "\\" + currentImage);

            pb.Image = b;
        }
        catch { };
    }

    private void button4_Click(object sender, EventArgs e)
    {
        try
        {
            index -= 1;
            string currentImage = IMAGENAME + index.ToString() + ".bmp";
            Bitmap b = new Bitmap(path + "\\" + currentImage);
            pb.Image = b;
        }
        catch { };
    }

首先使用DoubleBuffered属性。

GDI +非常慢,即使使用双缓冲,也仍然很慢。 我建议您找到一种替代方法,因为实际上没有解决“闪烁”的方法。

然而,有一件事,您实际上是在滴答句柄中加载图像的方式非常消耗CPU。

我建议您将所有所需图像的字节加载到某种类型的集合中,例如。 数组。 然后只需从图像字节创建位图。

此外,您甚至都没有处理当前图像,在为下一个图像分配新的内存之前应该这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM