0
我正在製作圖像查看器。但是我不知道如何在文件夾中設置文件的編號。 我必須設置CurrentImage編號。我描述的功能是Prev和Next按鈕。如何在C#文件夾中設置文件的編號
protected FileInfo [] imageFiles;
protected int pCurrentImage;
private void btnFileOpen_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "이미지 파일|*.jpg;*.bmp;*.gif;*.png";
if (dlg.ShowDialog() == DialogResult.OK)
{
Bitmap b = new Bitmap(dlg.FileName);
pictureBox1.Image = (Image)b.Clone();
pCurrentImage = 0;
DirectoryInfo di = new DirectoryInfo(dlg.FileName.Replace(dlg.SafeFileName, ""));
imageFiles = di.GetFiles("*.jpg;*.bmp;*.gif;*.png");
}
}
private void btnBefore_Click(object sender, EventArgs e)
{
--pCurrentImage;
if (imageFiles.Length > 0)
{
pCurrentImage = pCurrentImage == 0 ? imageFiles.Length - 1 : --pCurrentImage;
ShowCurrentImage();
}
}
private void ShowCurrentImage()
{
if (pCurrentImage >= 0 && pCurrentImage <= imageFiles.Length - 1)
{
pictureBox1.Image = Bitmap.FromFile(imageFiles[pCurrentImage].FullName);
//Invalidate();
pictureBox1.Invalidate();
}
}
private void btnNext_Click(object sender, EventArgs e)
{
++pCurrentImage;
if (imageFiles.Length > 0)
{
pCurrentImage = pCurrentImage == imageFiles.Length - 1 ? 0 : ++pCurrentImage;
ShowCurrentImage();
}
}