将png图像存储在c#容器中 [英] Store png images in c# container

查看:124
本文介绍了将png图像存储在c#容器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

我设计了一个应用程序,我想从多个目录中滑动图像和读取文本文件。我从文件夹中读取每个图像并将其存储在类型图像列表中。有超过3500个图像驻留在一个主目录中。所以我的问题是,当我尝试加载列表中的所有图像时;它给我异常Out of Memory。

我尝试了很多选项但是它的行为相同。在下面我复制了那个逻辑的代码片段。给我一些关于逻辑或数据类型错误的提示。

Hello!
I design an application in which i would like to slide images and read text files from multiple directory.I read each image from folder and store it in list of type image.There are more than 3500 images resides in one main directory.So my problem is that, when I am trying to load all images in list;it gives me exception 'Out of Memory'.
I tried lot options but it behaves same.Here below I copied code snippets of that logic.Give me some hint about error in logic or datatype used.

try {
	if (Directory.Exists(dirName)) {
	loadedImages = new List<Image>();//Create object of list which declared globally
	try {//Take file names of current directory into string array fileEntries
        	fileEntries = Directory.GetDirectories(dirName);
		for (int i = 0; i < fileEntries.Length; i++) {
               //Read file from each subdirectory.
		DirectoryInfo dir = new DirectoryInfo(fileEntries[i]);
		foreach (FileInfo f in dir.GetFiles("*.png")) {
		if (IsValidImage(f.FullName)) {//validate image by checking header of it
			loadedImages.Add(Image.FromFile(f.FullName));//Add image into list named as loadedImages
		}
	   }
        }
} catch (Exception exc) { MessageBox.Show(exc.Message + "Error while collecting images", "Get image error"); }

推荐答案

将所有这些图像一次加载到内存中毫无意义。将图像保留在目录中并创建一个仅包含图像名称的容器。更好的是,将所有文件重命名为与某些整数索引具有相同的名称,更好的是十六进制形式。然后,您可以让类具有对图像的索引访问权限。不再忘记在不再显示图像时忘记图像参考。这需要使引用无法访问,因此GAC最终可能会销毁它们。此外,当不再需要时,你需要 Dispose()每个图像。



它可以看起来类似于:

It makes absolutely no sense to load all those images in memory at once. Keep the images in the directory and create a container which holds only the image names. Better yet, rename all files to have the same names as some integer index, better in hexadecimal form. Then you can have the class with the indexed access to images. Don't forget to forget image references when an image is no longer displayed. This is needed to make the reference unreachable, so the GAC could eventually destroy them. Also, you would need to Dispose() each image when it is no longer needed.

It could look something like:
using System.Drawing;

// ...

    class ImageCollection {

        internal Image[] array = //... initialize new array when you populate
        
        Image this[int index] {
            get { // load on call:
                Image image = array[index];
                if (image != null) return image;
                // for example
                string imageFileName = string.Format("{0:X8}", index);
                image = Image.FromFile(imageFileName);
                array[index] = image;
                return image;
            }
        }

        internal void Forget(int index) {
            Image image = array[index];
            if (image == null) return;
            image.Dispose();
            array[index] = null;
        }

        // some methods for collection population ...

    }



在这里,您只需假设 Directory.GetFiles 为您提供在一个目录中具有文件名的文件的计数,其名称基于十六进制数00000000,00000001,... 0000000A等。最初,您使用 Count 创建空值的数组。每次在某个索引处需要一个文件,并发现该图像当前不在列表中时(它为空),您计算文件名并将其加载到数组元素,然后再使用它。当不再显示某些元素时,您可以忘记它们。这很重要:你不能同时装满它们。



-SA


Here, you just assume that Directory.GetFiles give you the count of files with file names in one directory with the names based on hexadecimal numbers 00000000, 00000001,… 0000000A, etc. Initially, you create the array with Count of null values. Each time you need a file at some index, and find that the image is not presently in the list (it's null), you calculate the file name and load it to the array element, and later use it. You can forget some elements when they are no longer displayed. This is important: you cannot keep them all loaded at the same time.

—SA


这篇关于将png图像存储在c#容器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆