我做了一个图片浏览,使用openfiledialog打开图片,代码如下:
string[] picArr;
int sum = 0;
int pageIndex = 0;
public void OpenFile(object sender, EventArgs e)
{
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.InitialDirectory = "d:\\MyDocument";
ofd1.Filter = "(*.jpg)|*.jpg|(*.*)|*.*";
ofd1.FilterIndex = 1;
ofd1.RestoreDirectory = true;
ofd1.Multiselect = true; //允许选择多个文件
if (ofd1.ShowDialog() == DialogResult.OK)
{
Image myImage = Image.FromFile(ofd1.FileName);
spc3.Panel1.BackgroundImage = myImage;
spc3.Panel1.Refresh();
picArr = ofd1.FileNames;
if (picArr.Count() == 1)
{
sum++;
pageIndex = sum;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
else
{
sum = picArr.Count();
pageIndex = sum;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
}
}其中textBox1是用来显示第几张图片,格式为1/4,我写的这种情况在一次性添加多张图片的时候,使用左右按钮浏览图片不会出问题。
向左的按钮,private void button13_Click(object sender, EventArgs e)
{
if (sum == 0)
{
pageIndex = sum;
}
else
{
if (pageIndex > 1)
{
string s = " " + (pageIndex - 1) + "/" + sum;
textBox1.Text = s;
pageIndex--;
} else
{
pageIndex = sum;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
int i = pageIndex - 1;
spc3.Panel1.BackgroundImage = Image.FromFile(picArr[i]);
}
}
向右的按钮, private void button14_Click(object sender, EventArgs e)
{
if (sum == 0)
{
pageIndex = sum;
}
else
{
if (pageIndex < sum)
{
string s = " " + (pageIndex + 1) + "/" + sum;
textBox1.Text = s;
pageIndex++;
}
else
{
pageIndex = 1;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
int i = pageIndex - 1;
spc3.Panel1.BackgroundImage = Image.FromFile(picArr[i]);
}
}
当图片一张一张载入的时候,picArr里面保存的图片就只有单张而已,我该如何把一张张加载的图片保存picArr里。
也就是说,我的问题是多张图片一起加载的时候,图片是保存在picArr里,这个没问题;而一张张加载的时候,图片要如何保存在picArr里。请高手指点
string[] picArr;
int sum = 0;
int pageIndex = 0;
public void OpenFile(object sender, EventArgs e)
{
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.InitialDirectory = "d:\\MyDocument";
ofd1.Filter = "(*.jpg)|*.jpg|(*.*)|*.*";
ofd1.FilterIndex = 1;
ofd1.RestoreDirectory = true;
ofd1.Multiselect = true; //允许选择多个文件
if (ofd1.ShowDialog() == DialogResult.OK)
{
Image myImage = Image.FromFile(ofd1.FileName);
spc3.Panel1.BackgroundImage = myImage;
spc3.Panel1.Refresh();
picArr = ofd1.FileNames;
if (picArr.Count() == 1)
{
sum++;
pageIndex = sum;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
else
{
sum = picArr.Count();
pageIndex = sum;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
}
}其中textBox1是用来显示第几张图片,格式为1/4,我写的这种情况在一次性添加多张图片的时候,使用左右按钮浏览图片不会出问题。
向左的按钮,private void button13_Click(object sender, EventArgs e)
{
if (sum == 0)
{
pageIndex = sum;
}
else
{
if (pageIndex > 1)
{
string s = " " + (pageIndex - 1) + "/" + sum;
textBox1.Text = s;
pageIndex--;
} else
{
pageIndex = sum;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
int i = pageIndex - 1;
spc3.Panel1.BackgroundImage = Image.FromFile(picArr[i]);
}
}
向右的按钮, private void button14_Click(object sender, EventArgs e)
{
if (sum == 0)
{
pageIndex = sum;
}
else
{
if (pageIndex < sum)
{
string s = " " + (pageIndex + 1) + "/" + sum;
textBox1.Text = s;
pageIndex++;
}
else
{
pageIndex = 1;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
int i = pageIndex - 1;
spc3.Panel1.BackgroundImage = Image.FromFile(picArr[i]);
}
}
当图片一张一张载入的时候,picArr里面保存的图片就只有单张而已,我该如何把一张张加载的图片保存picArr里。
也就是说,我的问题是多张图片一起加载的时候,图片是保存在picArr里,这个没问题;而一张张加载的时候,图片要如何保存在picArr里。请高手指点
所以就是用第三方控件ImageGear for .NETGdPicture SDKsLEAD TechnologiesDotImage 10.0最好的大牌子了我博客也有介紹
http://ufo-crackerx.blog.163.com/
数组的话 picArr = str1.Union(ofd1.FileNames).Distinct().ToArray();
这论坛不能编辑自己的帖子 太恶心了
不过用List<string>就没有这个烦恼了,每次Add()一下就行了
{
sum++;
pageIndex = sum;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
else
{
sum = picArr.Count();
pageIndex = sum;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}不明白这里搞个if-else做什么,没必要的,直接用else里面的情况就好了
string[] picArr={};
string[] FileNames = { "1", "2" };
picArr = picArr.Union(FileNames).Distinct().ToArray();
MessageBox.Show(picArr.Count().ToString()); 结果 2
测试了下没问题的
List<string> picArr=new List<string>();//改成了泛型
int sum = 0;
int pageIndex = 0;
public void OpenFile(object sender, EventArgs e)
{
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.InitialDirectory = "d:\\MyDocument";
ofd1.Filter = "(*.jpg)|*.jpg|(*.*)|*.*";
ofd1.FilterIndex = 1;
ofd1.RestoreDirectory = true;
ofd1.Multiselect = true; //允许选择多个文件
if (ofd1.ShowDialog() == DialogResult.OK)
{
Image myImage = Image.FromFile(ofd1.FileName[0]);//第一张
spc3.Panel1.BackgroundImage = myImage;
spc3.Panel1.Refresh();
picArr.AddRange(ofd1.FileNames);//添加图片
if (picArr.Count() == 1)
{
sum++;
pageIndex = sum;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
else
{
sum = picArr.Count();
pageIndex = sum;
string s = " " + pageIndex + "/" + sum;
textBox1.Text = s;
}
}
}
再者 你这个程序的图片加载是很慢的 没有预加载功能噢。。