处理大量的图片
本文关键字:处理 | 更新日期: 2023-09-27 17:54:24
我正在编写一个程序,打开一个folderBrowserDialog1
,然后使用
using string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
获取文件夹和子文件夹中的所有文件,随后程序将字符串数组拆分为多个列表,将数据过滤为4种类型
public static readonly List<string> ImageExtensions = new List<string> { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" };
public static readonly List<string> OfficeExtensions = new List<string> { ".DOC", ".DOCX", ".XLS", ".XLSX", ".PPT",".PPTX",".XLM",".PPS",".PPSX",".MDB" };
public static readonly List<string> VideoExtensions = new List<string> { ".FLV", ".AVI", ".MOV", ".MP4", ".MPG", ".WMV", ".3GP", ".ASF", ".RM", ".SWF",".MTS" };
public static readonly List<string> AudioExtensions = new List<string> { ".AAC", ".MP3", ".OGG", ".WMA", ".WAV" };
public static List<string> images = new List<string>();
public static List<string> audio = new List<string>();
public static List<string> videos = new List<string>();
我正在拍摄图片,以另一种形式显示它们的拇指代码如下:
public partial class frm_brsimag : MainForm
{
public static List<string> selimages = new List<string>();
int currPos = 0;
const int pageCount = 100;
public frm_brsimag()
{
InitializeComponent();
}
private void frm_brsimag_Load(object sender, EventArgs e)
{
menuStrip1.Hide();
}
private void LoadImages(int startFrom, int to)
{
flowLayoutPanel1.Controls.Clear();
for (int file = startFrom; file < startFrom + to; file++)
{
LinkLabel l = new LinkLabel();
l.Tag = FileManager.images[file];
l.Text = Path.GetFileNameWithoutExtension(FileManager.images[file]);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
PictureBox picBox = new PictureBox();
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
picBox.ContextMenuStrip = contextMenuStrip1;
picBox.Height = 125;
picBox.Width = 125;
picBox.LoadAsync(FileManager.images[file]);
//Label lbl = new Label();
// lbl.Text = Path.GetFileNameWithoutExtension(FileManager.images[file]);
flowLayoutPanel1.Controls.Add(picBox);
flowLayoutPanel1.Controls.Add(l);
l.LinkClicked += new LinkLabelLinkClickedEventHandler(LinkedLabelClicked);
//flowLayoutPanel1.Controls.Add(lbl);
}
}
private void LinkedLabelClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string filepath = ((LinkLabel)sender).Tag.ToString();
Process.Start("explorer.exe", "/select," + filepath);
}
private void btnNext_Click(object sender, EventArgs e)
{
if (FileManager.images.Count - currPos > pageCount)
{
LoadImages(currPos, pageCount);
currPos += pageCount;
}
else
{
LoadImages(currPos, FileManager.images.Count - currPos);
currPos += FileManager.images.Count - currPos;
}
}
private void btnPrev_Click(object sender, EventArgs e)
{
if (currPos >= pageCount)
currPos -= pageCount;
else
currPos = 0;
LoadImages(currPos, pageCount);
}
问题是:我有很多文件,有时要处理35K的图片。
我试图使next
和previous
按钮加载图片100 × 100的形式如图所示,以防止应用程序从内存中消耗大量的空间,但它不工作,当我加载程序与图片任务管理器显示,该程序消耗1.2 GB,当我按下next
按钮,它不断增加你可以帮助,并经过许多页面导航程序崩溃,谢谢!论坛的设计,所以我把事情弄清楚:https://i.stack.imgur.com/gdOYO.jpg
没关系,我以前的回答没有注意到你做了清除flowLayoutPanel1。也就是说,您可以在不删除处理程序的情况下清除其内容,也不处理一次性对象(控件是)-这至少是您的问题的一部分,因为附加事件处理程序将源实例保留在内存中。
因此,在清除控件之前,像这样分离处理程序(未经测试添加的代码-可能需要调整)。看看这是否有助于解决你的记忆问题。
private void LoadImages(int startFrom, int to)
{
foreach(var control in flowLayoutPanel1.Controls)
{
if(control is LinkLabel)
{
// Detach handlers
(control as LinkLabel).LinkClicked -= new LinkLabelLinkClickedEventHandler(LinkedLabelClicked);
}
// Dispose of controls we're about to clear out...
control.Dispose();
}
flowLayoutPanel1.Controls.Clear();
for (int file = startFrom; file < startFrom + to; file++)
{
LinkLabel l = new LinkLabel();
l.Tag = FileManager.images[file];
l.Text = Path.GetFileNameWithoutExtension(FileManager.images[file]);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
PictureBox picBox = new PictureBox();
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
picBox.ContextMenuStrip = contextMenuStrip1;
picBox.Height = 125;
picBox.Width = 125;
picBox.LoadAsync(FileManager.images[file]);
//Label lbl = new Label();
// lbl.Text = Path.GetFileNameWithoutExtension(FileManager.images[file]);
flowLayoutPanel1.Controls.Add(picBox);
flowLayoutPanel1.Controls.Add(l);
l.LinkClicked += new LinkLabelLinkClickedEventHandler(LinkedLabelClicked);
//flowLayoutPanel1.Controls.Add(lbl);
}
}