[英]Visual basic create graphics form picturebox then draw shapes
在表单加载事件中,我从一个名为PB_Pixel_Layout的空(无图像)图片框创建图形。
Private Sub Main_From_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Pixel_Graphics = PB_Pixel_Layout.CreateGraphics
End Sub
然后,通过SetPixel()绘制一堆填充的椭圆。 椭圆在画框上绘制,但是需要2个SetPixel()周期才能显示。 程序启动时,还会出现一个双窗口图像,导致滞后,不确定是什么引起了问题,但我认为我没有正确设置图形。 我试图通过在运行时创建位图而不是使用图片框来运行,但效果很好,因此问题仅限于从图片框创建图形。 我也尝试将图像加载到图片框属性中,但没有任何区别。
Private Sub SetPixel_Grid_Bitmap()
Pen_Grid = New Pen(Color.Gray)
Selected_Pen = New Pen(Color.Yellow)
'draw
For Col = 0 To intColumnCount
For Row = 0 To intRowCount
B_Color = Color.FromArgb(intPatternColorsRed(strhexPixelHexValue(intCounter + intBank)), intPatternColorsGreen(strhexPixelHexValue(intCounter + intBank)), intPatternColorsBlue(strhexPixelHexValue(intCounter + intBank)))
Brush_B = New SolidBrush(B_Color)
'// Grid
Pixel_Graphics.DrawEllipse(Pen_Grid, StartLocation.X + (Col * (intScale + 6)), StartLocation.Y + (Row * (intScale + 6)), intScale, intScale)
'// Fill with color
Pixel_Graphics.FillEllipse(Brush_B, StartLocation.X + (Col * (intScale + 6)) + 2, StartLocation.Y + (Row * (intScale + 6)) + 2, intScale - 4, intScale - 4)
'// Selected
If ArrPixelData_Array(intCounter)(P_Selected) = 1 Then
Pixel_Graphics.DrawEllipse(Selected_Pen, StartLocation.X + (Col * (intScale + 6)), StartLocation.Y + (Row * (intScale + 6)), intScale, intScale)
End If
intCounter = intCounter + 1
Next Row
Next Col
End Sub
这是更新
Public Sub RefreshDrawing()
bm = New Bitmap(PB_Pixel_Layout.Width, PB_Pixel_Layout.Height)
Using g As Graphics = Graphics.FromImage(BB)
g.SmoothingMode = SmoothingMode.AntiAlias
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
End Using
PB_Pixel_Layout.Image = bm ' assign drawn bitmap to picturebox
End Sub
我建议声明一个全局位图
Dim bm As Bitmap
并创建一个刷新功能,如(只是一般建议):
Public Sub RefreshDrawing()
bm = New Bitmap(Me.Drawing.Width, Me.Drawing.Height)
Using g = Graphics.FromImage(bm)
g.SmoothingMode = SmoothingMode.AntiAlias
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
Dim r as New RectangleF (200, 100, 400, 300)
g.FillEllipse(pDrwStyle.dstBrush, r) ' draw inside points (area)
g.DrawEllipse(pDrwStyle.dstPen, Rectangle.Round(r)) ' draw ellipse border
End Using
Me.PictureBox1.Image = bm ' assign drawn bitmap to picturebox
End Sub
在加载事件(可能还有一个刷新按钮)中,简单地输入:
Call RefreshDrawing()
这是绘制图形的非常快速和可靠的方式。
我建议不要使用Paint事件。 仅用于临时绘图,例如MouseMove模式下的绘图等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.