Quantcast
Viewing latest article 44
Browse Latest Browse All 48

paint event on picturebox

Hi Tommy

Good to see you back.

Tired and going to bed soon but here is a quickly put together example.

OK then, just to add ideas. Here is some GraphicsPath drawing. Useful in some circumstances and keeps Paint event handlers to minimum overheads. The text and shapes here are just random chosen examples. Items can be added/inserted/removed as needed from the GraphicsPaths.

This example draws to a Picturebox and to the Form irself as well.

Image may be NSFW.
Clik here to view.

Option Strict On
Option Explicit On
' Form1 with PictureBox1
Imports System.Drawing.Drawing2D
Public Class Form1
  Dim PBitems As New List(Of Item)
  Dim Formitems As New List(Of Item)
  Public Class Item
    Property GP As New GraphicsPath
    Property Pen As Pen = Pens.Black
    Property Outline As Boolean = True
    Property Fill As Boolean = True
    Property Brush As Brush = Brushes.White
  End Class
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' this just adds stuff to the PB
    ' GraphicsPath and the Form
    ' GraphicsPath
    PictureBox1.BackColor = Color.PaleGoldenrod
    Dim gp As New GraphicsPath
    gp.AddString("Sample FILLED Text in a PictureBox", New FontFamily("Arial"), FontStyle.Bold, 28, New Rectangle(20, 20, 300, 100), StringFormat.GenericDefault)
    PBitems.Add(New Item With {.Brush = Brushes.Green, .GP = gp, .Outline = False, .Fill = True})

    gp = New GraphicsPath
    gp.AddLine(New Point(20, 100), New Point(300, 100))
    PBitems.Add(New Item With {.Pen = New Pen(Color.Red, 3), .GP = gp})

    gp = New GraphicsPath
    gp.AddString("Second OUTLINED and FILLED text line in same GP", New FontFamily("Arial"), FontStyle.Bold, 32, New Rectangle(20, 120, 300, 160), StringFormat.GenericTypographic)
    PBitems.Add(New Item With {.Pen = New Pen(Color.Red, 2), .Brush = Brushes.LightBlue, .GP = gp, .Outline = True, .Fill = True})

    gp = New GraphicsPath
    gp.AddLine(New Point(20, 250), New Point(300, 250))
    PBitems.Add(New Item With {.Pen = New Pen(Color.Red, 3), .GP = gp})

    gp = New GraphicsPath
    gp.AddString("Text line only OUTLINED in same PICTUREBOX", New FontFamily("Arial"), FontStyle.Bold, 32, New Rectangle(20, 270, 300, 160), StringFormat.GenericTypographic)
    PBitems.Add(New Item With {.Pen = New Pen(Color.Maroon, 2), .Brush = Brushes.LightBlue, .GP = gp, .Outline = True, .Fill = False})

    gp = New GraphicsPath
    gp.AddEllipse(New Rectangle(PictureBox1.Right + 8, 40, 100, 200))
    Formitems.Add(New Item With {.Pen = New Pen(Color.Magenta, 2), .Brush = Brushes.Pink, .GP = gp, .Outline = True, .Fill = True})

    gp = New GraphicsPath
    gp.AddLines({New Point(PictureBox1.Right + 58, 100), New Point(PictureBox1.Right + 10, 160), New Point(PictureBox1.Right + 108, 160), New Point(PictureBox1.Right + 58, 100)})
    Formitems.Add(New Item With {.Pen = New Pen(Color.Black, 4), .GP = gp, .Outline = True, .Fill = False})
  End Sub
  Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    For Each i As Item In PBitems
      With i
        If .Outline Then e.Graphics.DrawPath(.Pen, .GP)
        If .Fill Then e.Graphics.FillPath(.Brush, .GP)
      End With
    Next
  End Sub
  Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
    For Each i As Item In Formitems
      With i
        If .Outline Then e.Graphics.DrawPath(.Pen, .GP)
        If .Fill Then e.Graphics.FillPath(.Brush, .GP)
      End With
    Next
  End Sub
End Class


Regards Les, Livingston, Scotland


Viewing latest article 44
Browse Latest Browse All 48

Trending Articles