1

嗨,我正在用 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);
        }
    }

现在这是我不知道在“下一步和返回”按钮中可以使用什么代码的问题。我知道它在循环。需要你们帮忙谢谢。。

4

3 回答 3

3

您需要某种文件枚举。一种方法是允许用户在OpenFileDialog.Multiselect属性中选择多个文件。OpenFileDialog公开一个包含所有选定文件名的属性 Files。

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]);
      }
   }
}

您可以为 next 和 previos 的单击事件实现事件处理程序,或者检查边框(因此一旦到达最后一个图像,您就不会超出它)或循环(如果用户在最后一个图像上单击 Next,则跳转到第一个)

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();
    }
于 2012-11-15T08:24:56.673 回答
2

这是有效的用户控制代码。您需要创建新的窗口窗体应用程序,创建名为 MyPictureViewer 的用户控件。向该控件添加图片框和 3 个按钮。将下面的代码粘贴到此用户控件代码隐藏中,挂钩单击事件,并将此控件添加到主窗体。希望这可以帮助

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]);
        }
    }
}
于 2012-11-15T09:22:19.293 回答
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.
于 2019-08-12T10:12:53.097 回答