你需要某種文件的枚舉。一種方法是允許用戶在OpenFileDialog.Multiselect
屬性中選擇多個文件。 OpenFileDialog
公開一個屬性包含所有選定文件名的文件。
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]);
}
}
}
你可以和旁邊或者previos檢查邊框(所以一旦你到達最後一個圖像,你不要去超越它)或循環(如果用戶點擊下一頁上實現事件處理程序,你的點擊事件最後一張圖片,跳轉到第一個)
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();
}
您可以從目錄中獲取圖像的所有名稱,並將它們放入數組中。然後在數組中使用id。 –