简体   繁体   中英

Visual basic create graphics form picturebox then draw shapes

On form load event I create graphics from an empty (no image) picturebox called 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

I then draw a bunch of filled ellipses through the SetPixel(). The ellipses get drawn on the picturebox but takes 2 cycles of the the SetPixel() to show. There is also a double window image when the program starts the causes a lag and not sure what is causing the problem but I assume I have not setup the graphics correctly. I tried to run by creating a bitmap at run time instead of using a picturebox and it worked fine so the issue is narrowed down to the create graphics from picturebox. I also tried to loading an image to the picturebox properties but did not make a difference.

 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

Here is the update

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

I would suggest to declare a global bitmap

Dim bm As Bitmap

And create a refresh function like (just a general suggestion):

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

And in you load event (and perhaps a Refresh button) put simply:

Call RefreshDrawing()

This is very fast and reliable way of drawing graphics.

I would suggest not to use Paint event. Only for temporary drawing, such as drawing in MouseMove mode and such.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM