如何从 WinForms 中的自定义控件中删除位图?
我有一个加载了生成的位图的表单。我希望用户能够按下按钮并更改显示的图形。我的问题是,如何删除当前显示的位图?
编辑:位图正在加载到 ImageBox 上(而不是直接加载到表单上),这是由 Hans Passant 用于绘制图形和滚动的 C# 面板
谢谢
I have a form that is loaded with a generated bitmap. I want the user to be able to press a button and change the graphic displayed. My question is, how can I delete the bitmap that is currently displayed?
Edit: The bitmap is loading onto an ImageBox (not directly onto the form) Which was kindly suggested by Hans Passant c# panel for drawing graphics and scrolling
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与leppie的回答原理相同。只不过您需要设置
ImageBox.Image
属性:这是因为以下代码(摘自 Hans 对您的回答)上一个问题):
当您将控件的
Image
属性设置为null
时,属性设置器会强制控件重新绘制自身(this.Invalidate() ;
)。当它重新绘制自身时,不会绘制任何图像,因为负责绘制控件的OnPaint
方法会在绘制控件之前验证mImage != null
。Same principle as leppie's answer. Except that you need to set the
ImageBox.Image
property instead:This works because of the following code (excerpted from Hans's answer to your previous question):
When you set the control's
Image
property tonull
, the property setter forces the control to repaint itself (this.Invalidate();
). When it repaints itself, no image is drawn because theOnPaint
method that is responsible for painting the control verifies thatmImage != null
before drawing it.以下应该有效:
The following should work: