【发布时间】:2013-05-05 22:16:02
【问题描述】:
我有一个表单,我在 Panel 中动态创建 8 个 PictureBoxes,其中显示 8 个不同的图像,每个 PictureBox 一个。图片为 50Kb Jpeg。
问题是我想让用户也看到 300 张图片,这就像 38 个面板,但每次我 LOAD 图像到 PictureBoxes 我的程序使用 5-7Mb 的 Ram 内存 更多。(我从 Windows 的任务管理器中看到)
我怎样才能减少这个增量?我的图片不是那么大,我只想显示预览,我不在乎质量。
这是我如何创建PictureBoxes:
private void buttonAdd_Click(object sender, EventArgs e)
{
Panel pan = new Panel();
PictureBox pic1 = new PictureBox();
pic1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
pic1.Location = new System.Drawing.Point(208, 5);
pic1.Name = "panel" + ctr.Count().ToString() + "pictureBox1";
pic1.Size = new System.Drawing.Size(100, 100);
pic1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
///
/// Same thing other 7 times with differents name and location
///
pan.Location = new System.Drawing.Point(12, 55);
pan.Name = "panel" + ctr.Count().ToString();
pan.Size = new System.Drawing.Size(1088, 129);
pan.TabIndex = 0;
pan.AllowDrop = true;
pan.BackColor = System.Drawing.SystemColors.ControlLight;
pan.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
pan.Controls.Add(pic1);
///
/// Same thing other 7 times
///
this.Controls.Add(pan);
}
这是我如何填充它们的:
void buttonClickEvent(object sender, EventArgs e)
{
string[] files= Directory.GetFiles("mylocalpath");
foreach(string x in files)
{
string imgIndex= x.Remove(0,x.LastIndexOf("_")+1).Remove(1);
PictureBox pic=pictureBox1;
//I get it everytime from imgIndex and sender's parent,
//too much code to show now
pic.ImageLocation = x;
}
}
【问题讨论】:
-
首先,不要相信任务管理器!
-
尝试覆盖
OnPaint和OnRefresh,这样您就可以加载图像,绘制它,然后忘记它。仅适用于静态未隐藏图像。
标签: c# image memory picturebox