【问题标题】:Why i'm getting exception out of memory when trying to display images in pictureBox?为什么我尝试在图片框中显示图像时出现内存异常?
【发布时间】:2017-02-02 23:43:08
【问题描述】:
int countFiles = 0;
private void timer1_Tick(object sender, EventArgs e)
{
    pictureBox1.Image = Image.FromFile(ff[countFiles]);
    countFiles++;
}

ff 是一个包含 40 个文件的列表。 定时器间隔设置为1000ms。

异常上线:

pictureBox1.Image = Image.FromFile(ff[countFiles]);

  System.OutOfMemoryException was unhandled
  HResult=-2147024882
  Message=Out of memory.
  Source=System.Drawing

【问题讨论】:

  • 您需要处理图像。

标签: c# .net winforms


【解决方案1】:

你需要从内存中释放之前的图像试试这个:

    private void timer1_Tick(object sender, EventArgs e)
    {
       if(pictureBox1.Image != null)
            pictureBox1.Image.Dispose();
        pictureBox1.Image = Image.FromFile(ff[countFiles]);
        countFiles++;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 2021-12-01
    相关资源
    最近更新 更多