来自文件流的位图 - XP / Vista上的内存不足异常

时间:2014-08-12 19:09:14

标签: c#

我正在使用文件流将位图加载到内存中,我可以从中操作它。代码如下:

try
{
    Bitmap tempimage;
    using (FileStream myStream = new FileStream(fullpath, FileMode.Open))
    {
        tempimage = (Bitmap)Image.FromStream(myStream);

    }
    tempimage.MakeTransparent(Color.FromArgb(0, 0, 255));
    this.pictureBox1.Image = tempimage;
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
catch
{
    MessageBox.Show("An error occured loading the selected image. Please check that it exists and is readable");
}

我不认为我在这里做了一些太有趣的事情,但这会在XP和Vista上引发内存不足(假设我删除了try / catch)。 Windows 8运行正常。 到目前为止,我已经检查过我是否传递了有效的文件名,并且图片没有损坏。

我在这里缺少什么?

3 个答案:

答案 0 :(得分:3)

图片处理完毕后,您仍在尝试使用它。

更正以下内容:

try
{
    Bitmap tempimage;
    using (FileStream myStream = new FileStream(fullpath, FileMode.Open))
    {
        tempimage = (Bitmap)Image.FromStream(myStream);
        tempimage.MakeTransparent(Color.FromArgb(0, 0, 255));
        this.pictureBox1.Image = tempimage;
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    }

}
catch
{
    MessageBox.Show("An error occured loading the selected image. Please check that it exists and is readable");
}

答案 1 :(得分:1)

尝试删除using语句:

http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx

您必须在图像的生命周期内保持流打开。

答案 2 :(得分:0)

请使用:

this.pictureBox1.Image = Image.FromFile(fullpath);
相关问题