2010-10-30 197 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