2010-10-30 117 views
0

我有一個按鈕設計的用戶控件。問題是我無法顯示圖像作爲背景。資源閱讀的問題

public partial class inout_buton : UserControl 
    { 
     Bitmap bmp; 
     public inout_buton() 
     { 

      InitializeComponent(); 

      try 
      { 
       Stream s = this.GetType().Assembly.GetManifestResourceStream("Network_Remote_Monitoring.but_verde.png"); 
       bmp = new Bitmap(s); 
       s.Close(); 
      } 
      catch 
      { 
       MessageBox.Show("it's bad"); 
      } 


      this.BackgroundImage = bmp; 
     } 
    } 

在這個例子中,Network_Remote_Monitoring是我namespaceand but_verde.png是我所需的背景。彈出的MessageBox總是出現=>不執行try語句。

你能找到問題嗎?

+0

您是否在屬性窗口中將「but_verde.png」圖片設置爲Embedded Resource? – jwaliszko 2010-10-30 16:51:23

回答

1
public partial class inout_buton : UserControl 
    { 
     Bitmap bmp; 
     public inout_buton() 
     { 

      InitializeComponent(); 

      try 
      { 
       bmp = new Bitmap(Network_Remote_Monitoring.Properties.Resources.but_verde); 
      } 
      catch 
      { 
       MessageBox.Show("it's bad"); 
      } 


      this.BackgroundImage = bmp; 
     } 
    } 
+0

'System.Drawing.Bitmap'不包含'png'的定義,並且沒有找到接受'System.Drawing.Bitmap'類型的第一個參數的擴展方法'png'(你缺少using指令或程序集參考?)。 – Alex 2010-10-30 16:53:36

+0

我必須將圖像轉換爲.jpg嗎? – Alex 2010-10-30 16:54:06

+0

沒關係,我刪除了.png,它工作得很好!謝謝 – Alex 2010-10-30 16:54:55