如果我理解得很好,您希望有两个按钮 (Next, Back)
我做了一个小项目,如果对你有帮助,我会很高兴。
如果你想要这个,请继续阅读:
首先我们必须声明imageCount 和List<string>:Imagefiles
所以我们有这个
List<string> Imagefiles = new List<string>();
int imageCount = 0;
imageCount 帮助使用按钮更改图像
Imagefiles 包含我们照片的所有图片路径
现在要更改照片,我们必须首先声明一个包含所有照片的路径。
我用FolderBrowserDialog
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
findImagesInDirectory(fbd.SelectedPath);
}
}
你会看到我使用findImagesInDirectory 并且这个方法不存在。我们必须创造它。
这个方法可以帮助我们过滤掉路径中的所有文件,只获取图片文件
private void findImagesInDirectory(string path)
{
string[] files = Directory.GetFiles(path);
foreach(string s in files)
{
if (s.EndsWith(".jpg") || s.EndsWith(".png")) //add more format files here
{
Imagefiles.Add(s);
}
}
try
{
pictureBox1.ImageLocation = Imagefiles.First();
}
catch { MessageBox.Show("No files found!"); }
}
我使用try,因为如果没有带有上述扩展名的图像文件,则存在代码会中断。
现在我们声明所有图像文件(如果它们存在)
下一步按钮
private void nextImageBtn_Click(object sender, EventArgs e)
{
if (imageCount + 1 == Imagefiles.Count)
{
MessageBox.Show("No Other Images!");
}
else
{
string nextImage = Imagefiles[imageCount + 1];
pictureBox1.ImageLocation = nextImage;
imageCount += 1;
}
}
上一个按钮
private void prevImageBtn_Click(object sender, EventArgs e)
{
if(imageCount == 0)
{
MessageBox.Show("No Other Images!");
}
else
{
string prevImage = Imagefiles[imageCount -1];
pictureBox1.ImageLocation = prevImage;
imageCount -= 1;
}
}
我相信我的代码会有所帮助。希望没有错误!