我用C#写了个图片浏览器,用的是picturebox控件,用一个ArrayList把一个文件夹里面的图片的路径存起来,然后就让picturebox显示出来,有两个按钮(前一幅,后一幅),可是,当去到第三幅的时候,就提示内存不足,呜啊。为什么这样子的啊?以下是代码部分:
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.Collections;
using System.IO;namespace 文件夹对话框_文件名
{
public partial class Form1 : Form
{
ArrayList tfilename = new ArrayList();
string path = null;
int j = 0;
int temp; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{ path = folderBrowserDialog1.SelectedPath;
DirectoryInfo dirinfo = new DirectoryInfo(path);
FileInfo[] filinfo = dirinfo.GetFiles();
temp = filinfo.Length-1;
for(int i=0;i<filinfo.Length;i++)
{ tfilename.Add((path + "\\"+filinfo[i].Name)); }
pictureBox1.Image=Image.FromFile((tfilename[0]).ToString());
}
}
private void button1_Click(object sender, EventArgs e) //前一幅
{
try
{
if (j == 0)
;
else
{
j = j - 1;
pictureBox1.Image = Image.FromFile((tfilename[j]).ToString()); }
}
catch
{
}
} private void button3_Click(object sender, EventArgs e) //后一幅
{
try
{
if (j >= temp)
;
else
{
j = j + 1;
pictureBox1.Image = Image.FromFile((tfilename[j]).ToString()); }
}
catch
{
}
}
哥哥们有空就帮我看看,谢谢啊,为什么picturebox会内存不足的?有什么好的解决办法吗?
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.Collections;
using System.IO;namespace 文件夹对话框_文件名
{
public partial class Form1 : Form
{
ArrayList tfilename = new ArrayList();
string path = null;
int j = 0;
int temp; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{ path = folderBrowserDialog1.SelectedPath;
DirectoryInfo dirinfo = new DirectoryInfo(path);
FileInfo[] filinfo = dirinfo.GetFiles();
temp = filinfo.Length-1;
for(int i=0;i<filinfo.Length;i++)
{ tfilename.Add((path + "\\"+filinfo[i].Name)); }
pictureBox1.Image=Image.FromFile((tfilename[0]).ToString());
}
}
private void button1_Click(object sender, EventArgs e) //前一幅
{
try
{
if (j == 0)
;
else
{
j = j - 1;
pictureBox1.Image = Image.FromFile((tfilename[j]).ToString()); }
}
catch
{
}
} private void button3_Click(object sender, EventArgs e) //后一幅
{
try
{
if (j >= temp)
;
else
{
j = j + 1;
pictureBox1.Image = Image.FromFile((tfilename[j]).ToString()); }
}
catch
{
}
}
哥哥们有空就帮我看看,谢谢啊,为什么picturebox会内存不足的?有什么好的解决办法吗?
解决方案 »
- 关于VS2010不能选择版本的问题
- 没有为该对象定义无参数的构造函数
- 如何生成DAT数据文件?
- StringBuilder生成HTML的问题
- 如何用代码来实现通过代理收发邮件?谢谢各位!
- 数据库是ACCESS的,我可以将图片成功地插入到数据库中,但是读出来的时候,图片显示不出来,显示的是一个大红叉
- visual studio 2005安装过程中遇到:Microsoft Visual Studio 2005 Team Suite - CHS 在安装期间遇到问题。安装没有正确完成。
- 多内码编程
- 怎么使用C#获取远程http://www.9630000.com/temp.txt,并将此文件获取并保存于本地盘当前文件夹下的temp.txt中。急用!!!谢谢!!!
- 请教各位:如何去掉OpenFileDialog上的快捷方式
- C#如何迭代成员?IEnumberable这个怎么用?
- 看完《C#入门经典》能学到什么程度?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO; namespace WindowsApplication1
{
public partial class Form1 : Form
{
ArrayList tfilename = new ArrayList();
string path = null;
int j = 0;
int temp;
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog(); public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{ path = folderBrowserDialog1.SelectedPath;
DirectoryInfo dirinfo = new DirectoryInfo(path);
FileInfo[] filinfo = dirinfo.GetFiles("*.jpg");
temp = filinfo.Length-1;
for(int i=0;i <filinfo.Length;i++)
{ tfilename.Add((path + "\\"+filinfo[i].Name)); }
pictureBox1.Image=Image.FromFile((tfilename[0]).ToString());
}
}
private void button1_Click(object sender, EventArgs e) //前一幅
{
try
{
if (j == 0)
;
else
{
j = j - 1;
pictureBox1.Image = Image.FromFile((tfilename[j]).ToString()); }
}
catch
{
}
} private void button3_Click(object sender, EventArgs e) //后一幅
{
try
{
if (j >= temp)
;
else
{
j = j + 1;
pictureBox1.Image = Image.FromFile((tfilename[j]).ToString()); }
}
catch
{
}
}
}
}
如果没有读取到任何文件, 也应该提示, 这是我最后修改的, 希望能给点帮助
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO; namespace WindowsApplication1
{
public partial class Form1 : Form
{
ArrayList tfilename = new ArrayList();
string path = null;
int j = 0;
int temp;
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
bool process = false; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
tfilename.Clear();
path = folderBrowserDialog1.SelectedPath;
DirectoryInfo dirinfo = new DirectoryInfo(path);
FileInfo[] filinfo = dirinfo.GetFiles();
if (filinfo.Length > 1)
{
foreach (FileInfo fi in filinfo)
{
if (fi.Extension == ".gif" || fi.Extension == ".jpg") //加上你的所有后缀
{
temp = filinfo.Length - 1;
for (int i = 0; i < filinfo.Length; i++)
{ tfilename.Add((path + "\\" + filinfo[i].Name)); }
pictureBox1.Image = Image.FromFile((tfilename[0]).ToString());
process = true;
}
}
}
else
{
MessageBox.Show("没有照片在这个目录下, 请检查");
process = false; }
}
}
private void button1_Click(object sender, EventArgs e) //前一幅
{
if (process)
{
try
{
if (j != 0)
{
j = j - 1;
pictureBox1.Image = Image.FromFile((tfilename[j]).ToString()); }
}
catch
{
}
}
else
{
MessageBox.Show("没有照片在这个目录下, 请检查");
}
} private void button3_Click(object sender, EventArgs e) //后一幅
{
if (process)
{
try
{
if (j > temp)
{
j = j + 1;
pictureBox1.Image = Image.FromFile((tfilename[j]).ToString()); }
}
catch
{
}
}
else
{
MessageBox.Show("没有照片在这个目录下, 请检查");
}
}
}
}
修改楼上代码
if (fi.Extension == ".gif" || fi.Extension == ".jpg") //加上你的所有后缀
{
temp = filinfo.Length - 1;
for (int i = 0; i < filinfo.Length; i++)
{ tfilename.Add((path + "\\" + filinfo[i].Name)); }
pictureBox1.Image = Image.FromFile((tfilename[0]).ToString());
process = true;
j++;//添加...
}
private void button2_Click(object sender, EventArgs e)//后
{
if (process)
{
try
{
if (j < temp)///修改