我现在正在制作图像查看器。但是我不知道如何在文件夹中设置文件的编号。我必须设置 CurrentImage 编号。我描述的功能是 Prev 和 Next 按钮。
受保护的 FileInfo[] 图像文件;
受保护的 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();
}
}