在表单加载事件中,我从一个名为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
答案 0 :(得分:1)
我建议声明一个全局位图
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模式下的绘图等。
答案 1 :(得分:0)
作为替代建议,您可以将其全部绘制到缓冲区,然后在需要时进行渲染。
赞:
Dim myContext As New BufferedGraphicsContext
Dim mybuff As System.Drawing.BufferedGraphics
(作为全局变量)
然后在开始绘制任何东西之前添加此*。
mybuff = myContext.Allocate(picturebox1.CreateGraphics, rectangle)
mybuff.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceOver
mybuff.Graphics.CompositingQuality = Drawing2D.CompositingQuality.AssumeLinear
mybuff.Graphics.SmoothingMode = Drawing2D.SmoothingMode.None
mybuff.Graphics.InterpolationMode = CType(Drawing2D.QualityMode.Low, Drawing2D.InterpolationMode)
mybuff.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
*仅第一行是必需的,其余为示例自定义配置。 矩形是缓冲区的大小,它至少必须与图像目标(picturebox1)一样大
要进入缓冲区,只需键入mybuff.Graphics ... 例如。
mybuff.Graphics.FillRectangle(colbrush,50,50, 4, 4)
mybuff.Graphics.FillEllipse(colbrush, 100, 100, 50, 30)
...
构建完图像后,需要像这样将其渲染/绘制到目标(picturebox1)上:
mybuff.Render
可以在代码中的任何位置调用绘制之后,无需直接绘制渲染。因此,取决于您何时绘制它以及绘制频率。
这样做比直接绘制到目标要快得多。
处理完我的buff和mycontext后,请立即处理。
[编辑] 忘了提一下,您也可以像这样将缓冲区呈现给其他目标:
Mybuff.Render(picturebox2.CreateGraphics)
Mybuff.Render(picturebox47.CreateGraphics)