日期:2014-05-18  浏览次数:21229 次

C# winform浏览图片的问题,懂得请进!
我做了一个图片浏览,使用openfiledialog打开图片,代码如下:
C# code

        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,我写的这种情况在一次性添加多张图片的时候,使用左右按钮浏览图片不会出问题。
向左的按钮,
C# code
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]);
            }
        }

向右的按钮,
C# code
        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里。请高手指点

------解决方案--------------------
网上的控件大把的哦
------解决方案--------------------
picArr = picArr.Union(ofd1.FileNames).Distinct().ToArray();
这论坛不能编辑自己的帖子 太恶心了
------解决方案--------------------
string[] picArr; 数组要遂个添加元素,得利用上下标,加载一张下标就往后挪一下(自加)
不过用List<string>就没有这个烦恼了,每次Add()一下就行了
------解决方案------------------