我有一個加載了生成的位圖的窗體。我希望用戶能夠按下按鈕並更改顯示的圖形。我的問題是,如何刪除當前顯示的位圖?如何從WinForms中的自定義控件中刪除位圖?
編輯:位圖加載到一個ImageBox(不直接到形式),這是好心由Hans帕桑特建議c# panel for drawing graphics and scrolling
感謝
我有一個加載了生成的位圖的窗體。我希望用戶能夠按下按鈕並更改顯示的圖形。我的問題是,如何刪除當前顯示的位圖?如何從WinForms中的自定義控件中刪除位圖?
編輯:位圖加載到一個ImageBox(不直接到形式),這是好心由Hans帕桑特建議c# panel for drawing graphics and scrolling
感謝
同原則作爲leppie的答案。除了你需要設置ImageBox.Image
屬性來代替:
myImageBox.Image = null;
這工作,因爲下面的代碼(從Hans's answer to your previous question摘錄):
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
if (mImage != null) e.Graphics.DrawImage(mImage, 0, 0);
base.OnPaint(e);
}
當控件的Image
屬性設置爲null
,屬性設置器強制控件重新繪製自己(this.Invalidate();
)。重新繪製時,不會繪製圖像,因爲負責繪製控件的OnPaint
方法會在繪製之前驗證mImage != null
。
下面應該工作:
Form.BackgroundImage = null;
嗨,不幸的是這不工作,因爲位圖不直接加載到窗體。請參閱我上面的編輯。謝謝。 – 2011-01-12 10:32:49
完美。謝謝。 – 2011-01-12 10:53:21