C# filestream中的位图-XP/Vista上出现内存不足异常

C# filestream中的位图-XP/Vista上出现内存不足异常,c#,C#,我正在使用文件流将位图加载到内存中,从内存中可以对其进行操作。其代码如下: try { Bitmap tempimage; using (FileStream myStream = new FileStream(fullpath, FileMode.Open)) { tempimage = (Bitmap)Image.FromStream(myStream); } tempimage.MakeTransparent(Color.FromA

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

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


我在这里遗漏了什么?

在图像已被“处置”后,您仍在尝试使用它

请使用以下命令更正:

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

在图像已被“处置”后,您仍在尝试使用它

请使用以下命令更正:

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语句:


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

请尝试删除using语句:

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

只需使用以下选项:

this.pictureBox1.Image = Image.FromFile(fullpath);
只要用这个:

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

这很有趣:)那为什么它在Windows8上还能工作呢?我当时的印象是,我将它分配给“使用”语句之外的位图,这样它就可以在内存中保持可访问性了。using语句是故意的(放错了?),因为应用程序可能会将多个图像加载到同一个picturebox1中并/或对它们进行操作,我不想泄漏内存。这很有趣:)为什么它在Windows 8上还能工作?我当时的印象是,我将它分配给“使用”语句之外的位图,这样它就可以在内存中保持可访问性了。using语句是故意的(放错了?),因为应用程序可能会将多个图像加载到同一个picturebox1和/或操纵它们,我不想泄露内存。什么文档,我们不需要臭文档;-)没错,但我总是发现
System.Drawing
非常不直观。什么文档,我们不需要糟糕的文档;-)没错,但我总是发现
System.Drawing
非常不直观。