绘制图形消失在VB.net(Drawing Graphics Disappear in VB.net

2019-07-03 18:01发布

我有一个简单的程序,你可以用FillEllipse函数和FillRectangle在屏幕上绘制。 我的问题是,当你在屏幕上,甚至一小部分拖到另一个窗口,该部分将被删除。 这种情况发生时,你在拖动另一个窗口,让去,并将其拖回了。 有没有什么办法解决这一问题?

Dim MyFormObject As Graphics = Me.CreateGraphics
        Select Case shape
            Case "Ellipse"
                MyFormObject.FillEllipse(brush, e.X - CInt(brushWidth / 2), e.Y - CInt(brushHeight / 2), brushWidth, brushHeight)
            Case "Rectangle"
                MyFormObject.FillRectangle(brush, e.X - CInt(brushWidth / 2), e.Y - CInt(brushHeight / 2), brushWidth, brushHeight)
        End Select

Answer 1:

你可以把一个PictureBox控件窗体上,绘制到不是,它不会被删除时,其他窗口油漆过它:

这样做一次,上的Form_Load什么:

pictureBox1.Image = new Bitmap(Width, Height);

得出:

Graphics.FromImage(pictureBox1.Image).FillRectangle(Brushes.Black, 0, 0, 100, 100);
pictureBox1.Refresh();


Answer 2:

你需要做的所有图纸中的Paint事件,每个控制被重新粉刷一次闪光。



Answer 3:

下面的代码可以让你用鼠标画(单击并拖动)矩形。 a添加PictureBox的形式。

Public Class Form1
  Private mpntMouseDown As Point

  Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim w As Integer = PictureBox1.Width
    Dim h As Integer = PictureBox1.Height
    Dim bmp As New Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    Using g As Graphics = Graphics.FromImage(bmp)
      Dim rct As New RectangleF(0, 0, w, h)
      Dim b As Brush = New System.Drawing.Drawing2D.LinearGradientBrush(rct, Color.White, Color.Blue, 0)
      g.FillRectangle(b, rct)
      g.DrawEllipse(Pens.Blue, New RectangleF(CInt(0.1 * w), CInt(0.2 * h), CInt(0.8 * w), CInt(0.6 * h)))
      g.FillEllipse(Brushes.Yellow, New RectangleF(CInt(0.1 * w) + 1, CInt(0.2 * h) + 1, CInt(0.8 * w) - 2, CInt(0.6 * h) - 2))
      Dim sft As New StringFormat
      sft.Alignment = StringAlignment.Center
      sft.LineAlignment = StringAlignment.Center
      g.DrawString("Sample Image", New Font(System.Drawing.FontFamily.GenericSerif, 14, FontStyle.Italic, GraphicsUnit.Point), Brushes.Red, rct, sft)
    End Using
    PictureBox1.Image = bmp
  End Sub

  Private Sub PictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Left Then
      mpntMouseDown = e.Location
    End If
  End Sub

  Private Sub PictureBox1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    If mpntMouseDown = Nothing Then Exit Sub
    Using g As Graphics = Graphics.FromImage(PictureBox1.Image, Bitmap)
      Dim rct As New Rectangle
      If mpntMouseDown.X < e.X Then
        rct.X = mpntMouseDown.X
        rct.Width = e.X - mpntMouseDown.X + 1
      Else
        rct.X = e.X
        rct.Width = mpntMouseDown.X - e.X + 1
      End If
      If mpntMouseDown.Y < e.Y Then
        rct.Y = mpntMouseDown.Y
        rct.Height = e.Y - mpntMouseDown.Y + 1
      Else
        rct.Y = e.Y
        rct.Height = mpntMouseDown.Y - e.Y + 1
      End If
      g.DrawRectangle(Pens.Black, rct)
    End Using
    mpntMouseDown = Nothing
    PictureBox1.Invalidate()
  End Sub
End Class


Answer 4:

@SLaks已经告诉你做的所有画在OnPaint方法。 这里有一个小的更多信息。 如果你想画一个表格上,你会重写OnPaint方法和做所有你画使用被传递到方法Graphics实例。 这里是关于主题的更多信息:

http://www.bobpowell.net/creategraphics.htm

http://www.bobpowell.net/picturebox.htm

Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaint(e)

    e.Graphics.FillEllipse(Brushes.Red, Me.ClientRectangle)
End Sub


Answer 5:

只是见识,真正帮助我在vb.net平局是这个例子vbnettuthut.blogspot.com它显示一个完整的例子来绘制顺利和迅速,工作示例和源代码,



文章来源: Drawing Graphics Disappear in VB.net