简体   繁体   English

C#Win。 表单-检索图像时内存不足

[英]C# Win. Form - Out of memory when retrieving an Image

Brief 简要

I want to display a PNG image in my PictureBox. 我想在我的PictureBox中显示一个PNG图像。 At runtime the software asks the path of the image from the user. 在运行时,软件向用户询问图像的路径。

Solution i am using 我正在使用的解决方案

This is my code uptill now 这是我现在的代码

picturebox1.Image = null;
OpenFileDialog ofDlg = new OpenFileDialog();
ofDlg.Filter = "Image files|*.png";
if (DialogResult.OK == ofDlg.ShowDialog())
{
     picturebox1.Image = Image.FromFile(ofDlg.FileName); //Out of memory.
}

Problem 问题

It was all working fine uptill now untill i got an image which was of 25.7 MB (8827 x 11350 pixels) . 现在一切正常,直到我得到25.7 MB(8827 x 11350 pixel)的图像为止。

I know you might all suggest that i should get a lighter version of this image BUT the problem is that this software is used to zoom the image to view the image in detail. 我知道你们可能都建议我应该得到该图像的较浅版本,但问题是该软件用于缩放图像以查看详细图像。 So i cannot resize it at any cost. 所以我不能不惜一切代价调整它的大小。

Now whenever i run the above code it gives me the exception 现在,只要我运行上面的代码,它就会给我一个例外

Out of memory. 记不清。

I do not understand what is the problem here because i have 8GB of RAM installed on my PC then how it is out of memory? 我不明白这里出了什么问题,因为我的PC上安装8GB的RAM ,那么它内存不足了吗? Below is my CPU usage at the time when this error message appeared. 以下是出现此错误消息时我的CPU使用率。

在此处输入图片说明

Follow the sugestions of all the comments, ie Make sure you are in 64 bit 遵循所有注释的提示,即确保您使用的是64位

But you need to also make sure you are disposing your images or you will run out of memory sooner or later anyway. 但是,您还需要确保要处置图像,否则迟早会耗尽内存。 The following is just an exmaple 以下只是一个例子

if(picturebox1.Image != null)
   picturebox1.Image.Dispose();

picturebox1.Image = null;

OpenFileDialog ofDlg = new OpenFileDialog();
ofDlg.Filter = "Image files|*.png";
if (DialogResult.OK == ofDlg.ShowDialog())
{
     picturebox1.Image = Image.FromFile(ofDlg.FileName); //Out of memory.
}

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

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