嗨,我正在用C#做一个简单的图像查看器。它有3个按钮第一个按钮包含“打开”,最后两个按钮是“后退和下一个”按钮和图片框。我已经完成了使用OpenFileDialog的打开按钮这是我在OpenFiledialog中的代码:
private void openButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JPEG|*.jpg|Bitmaps|*.bmp";
if(openFileDialog.ShowDialog()== DialogResult.OK)
{
pictureBox1.Image = Bitmap.FromFile(openFileDialog.FileName);
}
}
现在这个问题我不知道我可以在“next and back”按钮中使用哪些代码。我知道它在循环。需要你帮助的人谢谢你..
答案 0 :(得分:3)
您需要某种文件枚举。一种方法是允许用户在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]);
}
}
}
您可以为接下来的点击事件实现事件处理程序,并且可以检查边框(所以一旦到达最后一个图像就不会超出它)或循环(如果用户在最后一个图像上单击“下一步”,则跳转到第一个)
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();
}
答案 1 :(得分:2)
这是工作用户控制代码。您需要创建新的Window Forms Application,创建名为MyPictureViewer的用户控件。添加pictureBox和3个按钮到此控件。 将代码粘贴到此usercontrol代码隐藏,钩子单击事件,并将此控件添加到主窗体。希望这有帮助
public partial class MyPictureViewer : UserControl
{
protected string[] pFileNames;
protected int pCurrentImage = -1;
public MyPictureViewer()
{
InitializeComponent();
}
void btnPrevImage_Click(object sender, EventArgs e)
{
if (pFileNames.Length > 0)
{
pCurrentImage = pCurrentImage == 0 ? pFileNames.Length - 1 : --pCurrentImage;
ShowCurrentImage();
}
}
void btnNextImage_Click(object sender, EventArgs e)
{
if (pFileNames.Length > 0)
{
pCurrentImage = pCurrentImage == pFileNames.Length - 1 ? 0 : ++pCurrentImage;
ShowCurrentImage();
}
}
private void openButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
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]);
}
}
}
答案 2 :(得分:0)
Here what I did for "btnPrevImage" is some how easier:
*// this button is forward button*
private void btnForward_Click(object sender, EventArgs e)
{
if(currentIndex!=imageList1.Images.Count-1 && imageList1.Images.Count > 0)
{
pictureBox1.Image = imageList1.Images[currentIndex++];
}
}
*// this button is for Previews button*
private void btnPrevImage_Click(object sender, EventArgs e)
{
if (currentIndex!=0)
{
pictureBox1.Image = imageList1.Images[--currentIndex];
}
do not forget to declare currentIndex.