如何从 WinForms 中的自定义控件中删除位图?

发布于 2024-10-12 05:27:30 字数 251 浏览 4 评论 0原文

我有一个加载了生成的位图的表单。我希望用户能够按下按钮并更改显示的图形。我的问题是,如何删除当前显示的位图?

编辑:位图正在加载到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2024-10-19 05:27:30

与leppie的回答原理相同。只不过您需要设置 ImageBox.Image 属性:

myImageBox.Image = null;

这是因为以下代码(摘自 Hans 对您的回答)上一个问题):

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

Same principle as leppie's answer. Except that you need to set the ImageBox.Image property instead:

myImageBox.Image = null;

This works because of the following code (excerpted from 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);
}

When you set the control's Image property to null, the property setter forces the control to repaint itself (this.Invalidate();). When it repaints itself, no image is drawn because the OnPaint method that is responsible for painting the control verifies that mImage != null before drawing it.

不乱于心 2024-10-19 05:27:30

以下应该有效:

Form.BackgroundImage = null;

The following should work:

Form.BackgroundImage = null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文