2012-11-15 33 views
1

嗨,我正在做一個簡單的圖像查看器在C#中。它有3個按鈕第一個按鈕包含「打開」,最後兩個按鈕是「後退和下一個」和圖片框。我使用的OpenFileDialog這是我在打開文件對話框代碼開放的按鈕來完成:圖像查看器C#下一步按鈕

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

現在,這是問題,我沒有想法我可以使用代碼中的「下一個和後退」按鈕。我知道它在循環。需要你幫助的人謝謝你..

+0

您可以從目錄中獲取圖像的所有名稱,並將它們放入數組中。然後在數組中使用id。 –

回答

2

這裏是工作用戶控制代碼。您需要創建新的窗體窗體應用程序,創建名爲MyPictureViewer的用戶控件。將pictureBox和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]); 
     } 
    } 
} 
+0

此作品先生非常感謝你! – Jay

+0

先生是一個代碼,它將調整圖片框中的圖片大小 – Jay

+0

PictureBox具有屬性[SizeMode](http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox。 sizemode.aspx)你可以使用。您可以從其他圖像創建具有自定義大小的新圖像,或者如果您需要對圖像進行更高級的轉換,則必須從圖像獲取像素數組並對其進行處理。 – pawciu

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

你可以和旁邊或者previos檢查邊框(所以一旦你到達最後一個圖像,你不要去超越它)或循環(如果用戶點擊下一頁上實現事件處理程序,你的點擊事件最後一張圖片,跳轉到第一個)

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

@Ecarri先生,我嘗試使用你的代碼,但它不顯示圖像謝謝。 – Jay