在VB.net中绘制图形

在VB.net中绘制图形,vb.net,visual-studio-2010,graphics,draw,paint,Vb.net,Visual Studio 2010,Graphics,Draw,Paint,我有一个简单的程序,你可以用FillEllipse和FillRectangle在屏幕上绘制。我的问题是,当你在屏幕的一小部分上拖动另一个窗口时,该部分将被删除。当您将另一个窗口拖过、放开并将其拖回时,会发生这种情况。有办法解决这个问题吗 Dim MyFormObject As Graphics = Me.CreateGraphics Select Case shape Case "Ellipse" MyFormObject.

我有一个简单的程序,你可以用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

您需要在
Paint
事件中完成所有绘图,每次重新绘制控件时,该事件都会触发。

您可以在窗体上放置PictureBox控件并绘制到该控件上,其他窗口在其上绘制时,该控件不会被擦除:

在form_load或其他情况下,执行此操作一次:

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

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

下面的代码允许您使用鼠标(单击并拖动)绘制矩形。将
图片框添加到表单中

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
公共类表单1
私有mpntMouseDown作为点
私有子表单1_Load(发送方作为对象,e作为System.EventArgs)处理Me.Load
尺寸w为整数=PictureBox1.宽度
尺寸h为整数=PictureBox1.高度
将bmp调整为新位图(w、h、System.Drawing.Imaging.PixelFormat.Format32bppArgb)
使用g作为Graphics=Graphics.FromImage(bmp)
尺寸rct为新矩形F(0,0,w,h)
作为笔刷的尺寸b=新系统.Drawing.Drawing2D.LinearGradientBrush(rct,颜色.白色,颜色.蓝色,0)
g、 圆角矩形(b,rct)
g、 抽屉式(蓝色笔,新矩形F(CInt(0.1*w)、CInt(0.2*h)、CInt(0.8*w)、CInt(0.6*h)))
g、 圆角椭圆(画笔。黄色,新矩形F(CInt(0.1*w)+1,CInt(0.2*h)+1,CInt(0.8*w)-2,CInt(0.6*h)-2))
Dim sft作为新的StringFormat
sft.对齐=StringAlignment.居中
sft.LineAlignment=StringAlignment.Center
g、 DrawString(“示例图像”,新字体(System.Drawing.FontFamily.GenericSerif,14,FontStyle.Italic,GraphicsUnit.Point),画笔.红色,rct,sft)
终端使用
PictureBox1.Image=bmp
端接头
私有子PictureBox1\u MouseDown(发送方作为对象,e作为System.Windows.Forms.MouseEventArgs)处理PictureBox1.MouseDown
如果e.Button=Windows.Forms.MouseButtons.Left,则
mpntMouseDown=e.位置
如果结束
端接头
私有子PictureBox1\u MouseUp(发送者作为对象,e作为System.Windows.Forms.MouseEventArgs)处理PictureBox1.MouseUp
如果mpntMouseDown=Nothing,则退出Sub
使用g作为Graphics=Graphics.FromImage(PictureBox1.Image,位图)
将rct调整为新矩形
如果mpntMouseDown.X
@SLaks已经告诉您使用OnPaint方法进行所有绘制。这里有更多的信息。如果您试图在窗体上绘制,您将重写OnPaint方法,并使用传递给该方法的图形实例进行所有绘制。以下是有关该主题的更多信息:


绘画活动到底是什么?我将如何在其中绘画?我是图形新手您是直接绘制到屏幕(hdc 0)还是您自己的表单?编辑下面的答案,而不是每次需要绘制到持久图形(如pictureBox)时都创建新图形。或者,您可以在每次绘制事件中重新绘制,但这可能会很昂贵。如果PictureBox被另一个对象(如另一个窗体或另一个应用程序)覆盖,则您在此处绘制的所有图形都将消失。表单或控件的所有绘制都应该在OnPaint方法或绘制事件中进行。这很奇怪。当我测试代码时,隐藏表单不会丢失图形,但是最小化表单会丢失图形。我编辑了我的答案,以解决Chris Dunaway提出的问题。这为我的朋友解决了这个问题。他在表单加载中使用了Graphics.FromHwnd(pictureBox1.Handle)。更改为使用FromImage(pictureBox1.Image)后,它工作了!它现在显示表单加载时的图形,而不会消失!
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaint(e)
    e.Graphics.FillEllipse(Brushes.Red, Me.ClientRectangle)

End Sub