簡體   English   中英

圖像查看器C#下一個按鈕

[英]Image Viewer C# next Button

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

現在這是一個問題,我不知道我可以在“下一步和后退”按鈕中使用什么代碼。 我知道它正在循環。 需要您的幫助,謝謝。

您需要某種文件枚舉。 一種方法是允許用戶在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]);
      }
   }
}

您可以為下一個單擊事件實現事件處理程序,然后可以檢查邊框(因此一旦到達最后一個圖像,您就不會越過邊界)或循環(如果用戶在最后一個圖像上單擊“下一步”,則跳到第一個) )

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

這是有效的用戶控制代碼。 您將需要創建新的Window Forms Application,創建名為MyPictureViewer的用戶控件。 將pictureBox和3個按鈕添加到此控件。 將下面的代碼粘貼到此用戶控件代碼的后面,鈎住click事件,然后將此控件添加到主窗體中。 希望這可以幫助

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM