2012-10-04 59 views
0

下面顯示是我寫的編碼打開一個文件上顯示圖片框後,我單擊button方式:C#有關打開一個位圖文件中的winform

 OpenFileDialog dlg = new OpenFileDialog(); 
     private void button1_Click(object sender, EventArgs e) 
     { 
      dlg.Filter = "Image (*bmp)|*.bmp|All Files|*.*"; 
      SetLogo = 0; 

      if (dlg.ShowDialog() == DialogResult.OK) 
      { 

       this.pictureBox1.Image = (System.Drawing.Bitmap)Image.FromFile(dlg.FileName); 

       int nMaxBitmapHeigth = 800; 
       int nMaxBitmapWidth = 950; 
       string strOrgFullPath = dlg.FileName; 

       System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(strOrgFullPath); 
       System.Drawing.Bitmap bmpOrg = null; 

      // after this is the code for resizing the image to show on another picture box. 
     } 

後打開圖像顯示圖片框,我可以從combobox44選擇圖像的大小:

private void comboBox44_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int nMaxBitmapHeigth = 800; 
    int nMaxBitmapWidth = 950; 

    System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(dlg.FileName); 
    System.Drawing.Bitmap bmpOrg = null; 

// after this is the code for resizing the image to show on another picture box. 
} 

上按鈕1和combobox44兩種編碼是差不多的只是按鈕1使用戶能夠選擇從該對話框並combobox44圖像文件將使用從button1返回的圖像。

但編譯後出現錯誤。錯誤指向這一行「System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(dlg.FileName);」在combobox44上。錯誤消息是「路徑不是合法的形式」。怎麼了?爲什麼我不能使用button1中的圖像?

+1

「dlg.FileName」中有什麼?在選擇文件之前,這可能會被解僱。 (一個'OpenFileDialog'是一個可怕的地方,可以在按下OK之後存儲一段時間的路徑,我不確定這是一個偉大的想法,只要將它放在內存中就可以了) –

+1

我想dlg.Filename是在combobox44_SelectedIndex中的emtpy。附上一個debuigger並設置一個斷點(F9)來驗證。 – rene

+0

dlg.Filename與button1相同。我正在考慮button1選擇將在combobox44中使用。我可以這樣寫嗎? – Coolguy

回答

1

我想糾正的代碼如下:

string strOrgFullPath = ""; 
    private void button1_Click(object sender, EventArgs e) 
    { 
     dlg.Filter = "Image (*bmp)|*.bmp|All Files|*.*"; 
     SetLogo = 0; 

     if (dlg.ShowDialog() == DialogResult.OK) 
     { 

      this.pictureBox1.Image = (System.Drawing.Bitmap)Image.FromFile(dlg.FileName); 

      int nMaxBitmapHeigth = 800; 
      int nMaxBitmapWidth = 950; 
      strOrgFullPath = dlg.FileName; 

      System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(strOrgFullPath); 
      System.Drawing.Bitmap bmpOrg = null; 

     // after this is the code for resizing the image to show on another picture box. 
    } 


    private void comboBox44_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     int nMaxBitmapHeigth = 800; 
     int nMaxBitmapWidth = 950; 

     System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(strOrgFullPath); 
     System.Drawing.Bitmap bmpOrg = null; 

    // after this is the code for resizing the image to show on another picture box. 
    } 

    Try, Best of luck 
+0

我試過你的,但仍然是Pasad的錯誤信息。 – Coolguy

+0

當事件被觸發時檢查'strOrgFullPath'。它可能仍然是空的。 –

+0

是的,我原來把comboBox44.SelectedIndex = 0;在Form1_Load中。我認爲當Form1加載它檢測到combobox44中的索引更改並運行comboBox44_SelectedIndexChanged例程時,發現strOrgFullPath爲空。當我刪除comboBox44.SelectedIndex = 0;在Form1_Load中,已經很好了。 – Coolguy

3

直接拍攝影像從picturebox1你不需要從文件加載圖像,因爲你已經加載圖像picurebox1

private void comboBox44_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     int nMaxBitmapHeigth = 800; 
     int nMaxBitmapWidth = 950; 

     if (this.pictureBox1.Image != null) 
     { 
      System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(this.pictureBox1.Image, new System.Drawing.Size(nMaxBitmapHeigth, nMaxBitmapWidth)); 
      System.Drawing.Bitmap bmpOrg = null;  
     } 
     // after this is the code for resizing the image to show on another picture box. 
    } 
+0

+1只要你沒有更換'picturebox1'中的圖像後至少調整大小。否則,如果您再次調整小而大,則會丟失信息。 –

+0

@lc。是的,你是對的,但他可以將其信息傳遞給另一個變量,如果他需要它..謝謝 –

+1

是的,我認爲保持原始位圖在內存中是最好的解決方案,無論是在picturebox或其他地方。 –