Đây là lớp Picture.cs tôi:Bắt một 'ra khỏi bộ nhớ' ngoại lệ trong chương trình tương đối đơn giản này
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;
}
}
}
Và ở đây, tôi đang sử dụng lớp đó để lấy thông tin từ tất cả các tập tin trong một thư mục nhất định:
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);
}
}
}
Tôi nhận được một ngoại lệ bộ nhớ ngoài và tôi thực sự không biết tại sao. Đây là lần đầu tiên tôi làm điều gì đó như thế này vì vậy tôi khá mới mẻ đối với việc xử lý tệp theo lô, v.v.
Bất kỳ người trợ giúp nào? :)
Edit: tôi mở Task Manager để xem sử dụng bộ nhớ và khi tôi nhấn nút để chạy phương pháp này tôi nhận thấy việc sử dụng bộ nhớ của tôi tăng 100MB ~ mỗi giây.
Chỉnh sửa 2: Trong thư mục của tôi, tôi có khoảng 103 hình ảnh, mỗi hình ảnh là ~ 100kb. Tôi cần một giải pháp mà không quan trọng có bao nhiêu hình ảnh trong một thư mục. Có người đề nghị mở một hình ảnh, làm phép thuật của tôi, rồi đóng nó lại. Tôi không thực sự hiểu ý của anh ấy bằng cách 'đóng'.
Ai đó có thể đề xuất một cách tiếp cận khác không? :)
Chỉnh sửa 3: Vẫn nhận được ngoại lệ bộ nhớ, tôi đã thay đổi mã trong Picture.cs dựa trên đề xuất nhưng tôi không có ý tưởng. Bất kỳ giúp đỡ?
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;
}
}
}
Ngoài ra, tôi có nên mở một câu hỏi mới ngay bây giờ mà câu hỏi này đã phát triển một chút không?
Bạn có thể cung cấp cho chúng tôi dấu vết ngăn xếp của ngoại lệ OOM không? – Steven
Tôi làm cách nào để tìm ra điều đó? –
Một ngoại lệ được ném có một dấu vết ngăn xếp, nhưng không bao giờ nhớ rằng, vấn đề đã được phát hiện trong các phương thức calculatePictureHeight và calculatePictureWidth của bạn. – Steven