[英]Display many picture on ten pictureboxes one at a time
我的程序有十張圖片,顯示我想要的文件夾中的圖像。 添加下一個按鈕和上一個按鈕允許用戶瀏覽下十張圖片或最后十張圖片。 前十張圖片成功顯示,但下一個按鈕僅在文件夾有二十張圖片時才有效 ,如果圖片十五張則會崩潰。 這是我的代碼:
PictureBox[] myPicBoxArray = new PictureBox[10];
string path = @"\\Documents\Pictures\Camera";
private void Form1_Load(object sender, EventArgs e)
{
myPicBoxArray[0] = pictureBox1;
myPicBoxArray[1] = pictureBox2;
myPicBoxArray[2] = pictureBox3;
myPicBoxArray[3] = pictureBox4;
myPicBoxArray[4] = pictureBox5;
myPicBoxArray[5] = pictureBox6;
myPicBoxArray[6] = pictureBox7;
myPicBoxArray[7] = pictureBox8;
myPicBoxArray[8] = pictureBox9;
myPicBoxArray[9] = pictureBox10;
}
//Show button to display first ten pictures
private void showButton_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(path);
int i = 0;
foreach (string ofile in files)
{
myPicBoxArray[i].SizeMode = PictureBoxSizeMode.StretchImage;
myPicBoxArray[i].Image = Image.FromFile(files[i]);
i++;
if (i >= 10)
break;
}
}
private void nextButton_Click(object sender, EventArgs e)//The problem is here,
{
DirectoryInfo fileDir = new DirectoryInfo(path);
while (i2 < 10)
{
myPicBoxArray[i2].SizeMode = PictureBoxSizeMode.StretchImage;
if (i2 + 10 < picArrFileNames.Length)
{
myPicBoxArray[i2].Image = Image.FromFile(picArrFileNames[i2 + 10]);
}
}
}
沒有前一個按鈕的線索。
您可以簡單地將這些圖像存儲在ImageList
並使用屬性myImageList.Images.Count
來計算ImageList中的圖像數量,而不是使用有限的數組。
問題解決了.. :)或者使用Lists
並使用myList.Count
。
你可以這樣做:
DirectoryInfo dir = new DirectoryInfo(filePath);
foreach (FileInfo file in dir.GetFiles())
{
this.myImageList.Images.Add(Image.FromFile(file.FullName));
}
我不知道你為什么要這樣做:
if (i2 + 10 < picArrFileNames.Length)
{
myPicBoxArray[i2].Image = Image.FromFile(picArrFileNames[i2 + 10]);
}
但是,請嘗試使用此計數器,同時執行文件擴展名檢查以避免系統文件,如下面的MrGreen所建議的那樣。
private void nextButton_Click(object sender, EventArgs e)//The problem is here,
{
DirectoryInfo fileDir = new DirectoryInfo(path);
int count = fileDir.GetFiles().Length;
while (i2 < count)
{
myPicBoxArray[i2].SizeMode = PictureBoxSizeMode.StretchImage;
if (i2 + 10 < picArrFileNames.Length)
{
myPicBoxArray[i2].Image = Image.FromFile(picArrFileNames[i2 + 10]);
}
}
}
在這里的某個循環中使用它來進行擴展檢查在此處的循環中使用它來進行擴展檢查
string e = Path.GetExtension("YourFilePathHere");
if (e == ".jpeg")
{
//Do your stuff
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.