【发布时间】:2010-02-05 14:40:18
【问题描述】:
这是我的 Picture.cs 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
namespace SharpLibrary_MediaManager
{
public class Picture:BaseFile
{
public int Height { get; set; }
public int Width { get; set; }
public Image Thumbnail { get; set; }
/// <summary>
/// Sets file information of an image from a given image in the file path.
/// </summary>
/// <param name="filePath">File path of the image.</param>
public override void getFileInformation(string filePath)
{
FileInfo fileInformation = new FileInfo(filePath);
if (fileInformation.Exists)
{
Name = fileInformation.Name;
FileType = fileInformation.Extension;
Size = fileInformation.Length;
CreationDate = fileInformation.CreationTime;
ModificationDate = fileInformation.LastWriteTime;
Height = calculatePictureHeight(filePath);
Width = calculatePictureWidth(filePath);
}
}
public override void getThumbnail(string filePath)
{
Image image = Image.FromFile(filePath);
Thumbnail = image.GetThumbnailImage(40, 40, null, new IntPtr());
}
private int calculatePictureHeight(string filePath)
{
var image = Image.FromFile(filePath);
return image.Height;
}
private int calculatePictureWidth(string filePath)
{
var image = Image.FromFile(filePath);
return image.Width;
}
}
}
在这里,我使用该类从给定文件夹中的每个文件中提取信息:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SharpLibrary_MediaManager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string folderPath = @"D:\Images\PictureFolder";
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo folder = new DirectoryInfo(folderPath);
List<Picture> lol = new List<Picture>();
foreach (FileInfo x in folder.GetFiles())
{
Picture picture = new Picture();
picture.getFileInformation(x.FullName);
lol.Add(picture);
}
MessageBox.Show(lol[0].Name);
}
}
}
我遇到了内存不足异常,我真的不知道为什么。这是我第一次做这样的事情,所以我对批处理文件处理等很陌生。
有帮助吗? :)
编辑: 我打开任务管理器查看内存使用情况,当我按下按钮运行该方法时,我注意到我的内存使用量每秒增加 100mb~。
编辑 2: 在我的文件夹中,我有大约 103 张图片,每张图片约为 100kb。 我需要一个解决方案,不管文件夹中有多少图像。有人建议打开图像,施展魔法,然后关闭它。我真的不明白他所说的“关闭”是什么意思。
有人可以推荐一种不同的方法吗? :)
编辑 3: 仍然出现内存不足异常,我已经根据建议更改了 Picture.cs 中的代码,但我没有想法。有什么帮助吗?
public override void getFileInformation(string filePath)
{
FileInfo fileInformation = new FileInfo(filePath);
using (var image = Image.FromFile(filePath))
{
if (fileInformation.Exists)
{
Name = fileInformation.Name;
FileType = fileInformation.Extension;
Size = fileInformation.Length;
CreationDate = fileInformation.CreationTime;
ModificationDate = fileInformation.LastWriteTime;
Height = image.Height;
Width = image.Width;
}
}
}
另外,既然这个问题已经增长了一点,我应该打开一个新问题吗?
【问题讨论】:
-
你能给我们一个OOM异常的堆栈跟踪吗?
-
我怎样才能找到它并提供它?
-
抛出的异常有堆栈跟踪,但没关系,问题已经在您的 calculatePictureHeight 和 calculatePictureWidth 方法中发现。
-
你能告诉我们你的程序究竟是什么时候崩溃的吗?了解代码中问题发生的确切位置对我们很有帮助。 (别忘了在你的
Picture类上实现IDisposable接口,因为它包含一个Image对象。) -
您是否考虑过使用 Visual Studio 中的分析器或 CLRProfiler (http://msdn.microsoft.com/en-us/library/ms979205.aspx) 来了解正在分配的对象以及原因?
标签: c# .net image io out-of-memory