c# – 在这个相对简单的程序中获得“内存不足”异常

前端之家收集整理的这篇文章主要介绍了c# – 在这个相对简单的程序中获得“内存不足”异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的Picture.cs类:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Drawing;
  7.  
  8. namespace SharpLibrary_MediaManager
  9. {
  10. public class Picture:BaseFile
  11. {
  12. public int Height { get; set; }
  13. public int Width { get; set; }
  14. public Image Thumbnail { get; set; }
  15.  
  16. /// <summary>
  17. /// Sets file information of an image from a given image in the file path.
  18. /// </summary>
  19. /// <param name="filePath">File path of the image.</param>
  20. public override void getFileInformation(string filePath)
  21. {
  22. FileInfo fileInformation = new FileInfo(filePath);
  23. if (fileInformation.Exists)
  24. {
  25. Name = fileInformation.Name;
  26. FileType = fileInformation.Extension;
  27. Size = fileInformation.Length;
  28. CreationDate = fileInformation.CreationTime;
  29. ModificationDate = fileInformation.LastWriteTime;
  30. Height = calculatePictureHeight(filePath);
  31. Width = calculatePictureWidth(filePath);
  32. }
  33. }
  34.  
  35. public override void getThumbnail(string filePath)
  36. {
  37. Image image = Image.FromFile(filePath);
  38. Thumbnail = image.GetThumbnailImage(40,40,null,new IntPtr());
  39. }
  40.  
  41. private int calculatePictureHeight(string filePath)
  42. {
  43. var image = Image.FromFile(filePath);
  44. return image.Height;
  45. }
  46.  
  47. private int calculatePictureWidth(string filePath)
  48. {
  49. var image = Image.FromFile(filePath);
  50. return image.Width;
  51. }
  52. }
  53. }

在这里,我正在使用该类从给定文件夹中的每个文件提取信息:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace SharpLibrary_MediaManager
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. string folderPath = @"D:\Images\PictureFolder";
  21.  
  22. private void button1_Click(object sender,EventArgs e)
  23. {
  24. DirectoryInfo folder = new DirectoryInfo(folderPath);
  25. List<Picture> lol = new List<Picture>();
  26. foreach (FileInfo x in folder.GetFiles())
  27. {
  28. Picture picture = new Picture();
  29. picture.getFileInformation(x.FullName);
  30. lol.Add(picture);
  31. }
  32.  
  33. MessageBox.Show(lol[0].Name);
  34. }
  35. }
  36. }

我得到了Out Of Memory异常,我真的不知道为什么.这是我第一次做这样的事情所以我对批处理文件处理等很陌生.

有帮助吗?

猜你在找的C#相关文章