2010-12-12 117 views
4

這個我的我的代碼片段....顯示圖像的面板

public void btn_browse_Click_Click(object sender, EventArgs e) 
    { 

     try 
     { 
      OpenFileDialog open = new OpenFileDialog(); 
      open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; 
      if (open.ShowDialog() == DialogResult.OK) 
      { 
       scan.Enabled = true; 
       pic = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); 
       pic2 = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);         

       pic = new Bitmap(open.FileName); 
       pic2 = new Bitmap(open.FileName); 

       pictureBox1.Image = pic; 
       pictureBox2.Image = pic2; 
       pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 
       textBox1.Text = open.FileName; 
       pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 



      } // end of if opendialog 


     } // end of try 

     catch (Exception) 
     { 
      throw new ApplicationException("Failed loading image"); 
     } 


    } 

這樣的問題:我只能對我的顯示圖像的面板,而不是一個圖片上瀏覽?

回答

9

您可以將面板的BackGroundImage設置爲圖像。
panel.BackgroundImage = Image.FromFile(open.FileName);
應該這樣做。

0

是的。 Panel class有一個名爲BackImageUrl的成員。只需指定您想用作背景的圖片的網址即可。

3

基本上PictureBox顯示圖像Panel用於繪製在它(曲線,直線,矩形,....)

因此,我建議最好使用圖片框,但如果你想在面板中顯示圖像。

  1. 您可以繪製on paint事件
    OR
  2. 使用BackgroundImage財產。