2011-05-11 25 views
2

而不是使用'背景'屬性設置圖像,我想在面板上使用圖形類繪製圖像。我怎樣才能在C#.Net中做到這一點?在c#中的圖形面板#

+0

什麼是您的平臺?的WinForms? WPF? Silverlight的? – Gabe 2011-05-11 04:33:59

+0

你是說你想在面板上手動繪製而不是將圖像分配給背景屬性?你爲什麼試圖重新發明輪子?您可以使用背景屬性在運行時設置和清除圖像。我想我沒有看到好處。你能解釋一下這個推理嗎? – IAmTimCorey 2011-05-11 04:39:57

回答

1

你可以試試下面的一段代碼。

public class ImagePanel:Panel 
{ 
    private Image image; 

    public Image Image 
    { 
     get { return image; } 
     set 
     { 

      image = value; 
      Refresh(); 
     } 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     if(Image!=null) 
     { 
      e.Graphics.DrawImage(this.Image,Point.Empty); 
     } 
     base.OnPaint(e); 
    } 
}