您需要某种文件枚举。一种方法是允许用户在OpenFileDialog.Multiselect
属性中选择多个文件。OpenFileDialog
公开一个包含所有选定文件名的属性 Files。
class MyPictureViewer
{
protected string[] pFileNames;
protected int pCurrentImage = -1;
private void openButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JPEG|*.jpg|Bitmaps|*.bmp";
if(openFileDialog.ShowDialog()== DialogResult.OK)
{
pFileNames = openFileDialog.FileNames;
pCurrentImage=0;
ShowCurrentImage();
}
}
protected void ShowCurrentImage()
{
if(pCurrentImage >= 0 && pCurrentImage < pFileNames.Length-1)
{
pictureBox1.Image = Bitmap.FromFile(pFileNames[pCurrentImage]);
}
}
}
您可以为 next 和 previos 的单击事件实现事件处理程序,或者检查边框(因此一旦到达最后一个图像,您就不会超出它)或循环(如果用户在最后一个图像上单击 Next,则跳转到第一个)
void btnNextImage_Click(object sender, EventArgs e)
{
++pCurrentImage;
//check if this was last image in list
if(pCurrentImage >= pFileNames.Length)
pCurrentImage = pFileNames.Length == 0? -1 : 0;//if this was last image, go to first image
ShowCurrentImage();
}
void btnPrevImage_Click(object sender, EventArgs e)
{
--pCurrentImage;
//check if this was first image in list
if (pCurrentImage < 0)
pCurrentImage = pFileNames.Length == 0 ? -1 : pFileNames.Length -1;//if this was first image, go to last image
ShowCurrentImage();
}