我正在编写一个打开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
按钮时,它会继续增加你可以帮忙吗?经过多次页面导航,程序崩溃了,谢谢!
论坛的设计让我说清楚:http://i.stack.imgur.com/gdOYO.jpg
答案 0 :(得分:0)
没关系,我之前的回答没有注意到你确实清除了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);
}
}