【问题标题】:How do I remove the bitmap from a custom control in WinForms?如何从 WinForms 中的自定义控件中删除位图?
【发布时间】:2011-06-07 17:55:31
【问题描述】:

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

编辑:位图正在加载到 ImageBox 上(而不是直接加载到表单上),这是 Hans Passant c# panel for drawing graphics and scrolling 的善意建议

谢谢

【问题讨论】:

    标签: c# winforms controls bitmap panel


    【解决方案1】:

    与 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。

    【讨论】:

      【解决方案2】:

      以下应该有效:

      Form.BackgroundImage = null;
      

      【讨论】:

      • 嗨,不幸的是,这不起作用,因为位图没有直接加载到表单中。请参阅我上面的编辑。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2011-08-27
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多