绘图图形在 VB.net 中消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13334210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing Graphics Disappear in VB.net
提问by WillumMaguire
I have a simple program that you can draw on the screen with FillEllipse and FillRectangle. My problem is that when you drag another window over even a small portion of the screen, that part will be erased. This happens when you drag the other window over, let go, and drag it back off. Is there any way to fix this?
我有一个简单的程序,您可以使用 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
回答by gordy
You can put a PictureBox control on your form and draw to that instead and it won't be erased when other windows paint over it:
你可以在你的窗体上放置一个 PictureBox 控件并绘制到它上面,当其他窗口在它上面绘制时它不会被擦除:
do this once, on form_load or something:
做一次,在 form_load 或其他东西上:
pictureBox1.Image = new Bitmap(Width, Height);
to draw:
绘制:
Graphics.FromImage(pictureBox1.Image).FillRectangle(Brushes.Black, 0, 0, 100, 100);
pictureBox1.Refresh();
回答by SLaks
You need to do all of your drawing in the Paintevent, which fires each time your control gets repainted.
您需要在Paint事件中完成所有绘图,每次重新绘制控件时都会触发该事件。
回答by SSS
The following code allows you to draw a rectangle with the mouse (click and drag). Add a PictureBoxto a form.
以下代码允许您使用鼠标(单击并拖动)绘制一个矩形。将 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
回答by Chris Dunaway
@SLaks already told you to do all painting in the OnPaint method. Here's a little more information. If you're trying to draw on a form, you would override the OnPaint method and do all you painting using the Graphics instance that is passed into the method. Here is more information on the topic:
@SLaks 已经告诉您在 OnPaint 方法中进行所有绘画。这里有更多信息。如果您尝试在窗体上绘图,您将覆盖 OnPaint 方法并使用传递给该方法的 Graphics 实例完成所有绘图。以下是有关该主题的更多信息:
http://www.bobpowell.net/creategraphics.htm
http://www.bobpowell.net/creategraphics.htm
http://www.bobpowell.net/picturebox.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
回答by Samuel
just a insight, what really helped me with draw in vb.net was this example vbnettuthut.blogspot.comit show a complete example to draw smooth and fast, with working example and source cod
只是一个见解,真正帮助我在 vb.net 中绘制的是这个例子vbnettuthut.blogspot.com它展示了一个完整的例子来平滑和快速地绘制,带有工作示例和源代码

