到目前为止,我的代码有效,除了上一个和下一个按钮仅适用于2张图像。为什么?我不明白,列表应该包含所有内容,然后为什么呢?
如果我添加10张图片,我想一直按上一首和下一首。
int PageNumber = 0;
// A list of image filenames to display. You could populate this by
reading filenames from disk
List<string> ImageFilenames = new List<string>();
private void btnAdd_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
open.Multiselect = true;
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg;
*.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
pctBox.SizeMode = PictureBoxSizeMode.CenterImage;
// display image in picture box
ImageFilenames.Add(open.FileName);
pctBox.Image = new Bitmap(open.FileName);
// image file path
//textBox1.Text = open.FileName;
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
//if there is no more pages
if (PageNumber == ImageFilenames.Count - 1)
{
// Move to the next page
--PageNumber;
// Load up the PictureBox with the new image.
pctBox.Image = new Bitmap(ImageFilenames[PageNumber]);
}
}
private void btnNext_Click(object sender, EventArgs e)
{
// If we're not yet on the last page...
if (PageNumber < ImageFilenames.Count - 1)
{
// Move to the next page
++PageNumber;
// Load up the PictureBox with the new image.
pctBox.Image = new Bitmap(ImageFilenames[PageNumber]);
}
}
答案 0 :(得分:1)
稍微改变了您的逻辑。请找到代码以供参考
int nTotalNumber = 0;
int nCurrentItem = 0;
List<string> ImageFilenames = new List<string>();
private void LoadImage()
{
using (OpenFileDialog open = new OpenFileDialog())
{
open.Multiselect = true;
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp;*.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
if (open.ShowDialog() == DialogResult.OK)
{
string sFileName = open.FileName;
ImageFilenames = open.FileNames.ToList();
}
pbBox.Image = Image.FromFile(ImageFilenames[0]);
}
if (ImageFilenames.Count > 0)
nTotalNumber = ImageFilenames.Count;
}
private void btnPrevious_Click(object sender, EventArgs e)
{
nCurrentItem--;
if (nCurrentItem < 0)
nCurrentItem = 0;
else if (nCurrentItem < nTotalNumber)
pbBox.Image = Image.FromFile(ImageFilenames[nCurrentItem]);
}
private void btnNext_Click(object sender, EventArgs e)
{
nCurrentItem++;
if (nCurrentItem > nTotalNumber)
nCurrentItem = nTotalNumber;
else if (nCurrentItem < nTotalNumber)
pbBox.Image = Image.FromFile(ImageFilenames[nCurrentItem]);
}