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

本文关键字:XP Vista 内存不足 异常 位图 文件 | 更新日期: 2023-09-27 18:17:37

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

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运行得很好。到目前为止,我已经检查了我正在传递一个有效的文件名,并且图像没有损坏。

我在这里错过了什么?

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

在图片已经被"处置"之后,您还在尝试使用它。

更正如下:

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");
}

尝试删除using语句:

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

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

就用这个:

this.pictureBox1.Image = Image.FromFile(fullpath);