从PictureBox获取位图 - 一个对象的新实例 [英] Get bitmap from PictureBox - new instance of an object

查看:142
本文介绍了从PictureBox获取位图 - 一个对象的新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无瑕疵的工作代码,用于将部分屏幕放入方形PictureBox中。

我有一个无瑕疵的工作代码,可以用笔在绘图盒上绘制。

我完全无法理解,为什么我无法在画框中的位图上画笔 - 它只能在灰色背景上绘制。

解决方案(也按照谷歌)看起来很简单,但任何尝试从PictureBox.Image获取gg图形的方法都会导致(创建表单时发生错误,详情请参阅Exception.InnerException。错误是:值不能为null
参数名称:image和使用'New'关键字创建对象的新实例...)。

I have a "flawless" working code for putting a part of screen into square PictureBox.
I have a "flawless" working code for drawing on the picturebox with a pen.
I totally fail to understand, why I cannot get the pen draw on the bitmap in the PictureBox - it only draws on grey background.
The solution (also as per google) is seemingly simple, but ANY way to attempt to get gg graphics from PictureBox.Image results in ("An error occurred creating the form. See Exception.InnerException for details. The error is: Value cannot be null. Parameter name: image" and "Use the 'New' keyword to create new instance of an object...").

我认为麻烦在于我将位图定义为私有子外部,但我无法弄清楚正确的概念......

I think that the trouble is in the way I define the bitmap as private outside a sub, but I cannot figure out correct concept...

Private bit As New System.Drawing.Bitmap(250, 250)
Private gg As Graphics = Graphics.FromImage(Me.PictureBox1.Image)
'            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'            ^^^^^^^^^^ HERE IS THE PROBLEM ^^^^^^^^^^^^^^^^^^^^^^
'            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Private br As New Pen(Color.Red)
Private dwn As Boolean
Private lst As Point
Private firstrun As Boolean = True
Dim Tloustka As Integer = 10
Dim Barva As Integer

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    'gg = Graphics.FromImage(Me.PictureBox1.Image)
    dwn = True
End Sub


Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    If dwn = True Then
        Dim s As Integer
        Dim xy As Point
        Dim br2 As New SolidBrush(Color.FromName("White"))
        s = Tloustka
        br.Color = Color.FromName("White")
        br.Width = Tloustka
        xy.X = e.X
        xy.Y = e.Y
        If firstrun = True Then
            lst = xy
            firstrun = False
        End If
        gg.FillEllipse(br2, xy.X - CLng(s / 2), xy.Y - CLng(s / 2), s, s)
        gg.DrawLine(br, lst, xy)
        lst = xy
        PictureBox1.Image = bit
    End If
End Sub

etc. etc. etc.


推荐答案

不要存储图形对象。当你需要绘制一些东西时,从位图创建它:

Don't store the Graphic object. Just create it from the bitmap when you need to draw something:

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
  If dwn = True Then
    Using g As Graphics = Graphics.FromImage(bit)
      Dim s As Integer
      Dim xy As Point
      Using br2 As New SolidBrush(Color.FromName("White"))
        s = Tloustka
        br.Color = Color.FromName("White")
        br.Width = Tloustka
        xy.X = e.X
        xy.Y = e.Y
        If firstrun = True Then
          lst = xy
          firstrun = False
        End If
        g.FillEllipse(br2, xy.X - CLng(s / 2), xy.Y - CLng(s / 2), s, s)
        g.DrawLine(br, lst, xy)
      End Using
    End Using
    lst = xy
    PictureBox1.Invalidate()
  End If
End Sub

然后使用PictureBox的绘画事件来显示图像:

Then use the paint event of the PictureBox to show the image:

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
  e.Graphics.DrawImage(bit, Point.Empty)
End Sub

确保您销毁图形对象。

这篇关于从PictureBox获取位图 - 一个对象的新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆