Draw a Grid on a UserControl

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom

    #1

    Draw a Grid on a UserControl

    Hey,

    I have tried drawing a grid onto my control using the following:


    Private Sub VGUI_Grid(...) Handles MyBase.Paint
    Dim new_bitmap As Bitmap

    ' Make a new bitmap that fits the PictureBox.
    new_bitmap = New Bitmap(Me.Width , Me.Height)
    m_BufferGraphic s = Graphics.FromIm age(new_bitmap)

    ' Clear the new bitmap.
    m_BufferGraphic s.Clear(Me.Back Color)

    ' Draw the positioning grid.
    DrawGrid(new_bi tmap)

    ' Copy the existing bitmap's contents into
    ' the new bitmap.
    If Not (m_BufferBitmap Is Nothing) Then
    m_BufferGraphic s.DrawImage(m_B ufferBitmap, 0, 0)
    End If

    ' Save the new bitmap and graphics objects.
    m_BufferBitmap = new_bitmap
    Me.BackgroundIm age = m_BufferBitmap
    End Sub


    Private Sub DrawGrid(ByVal bm As Bitmap)
    For Y As Integer = 0 To bm.Height - 1 Step GRID_SPACING
    For X As Integer = 0 To bm.Width - 1 Step _
    GRID_SPACING
    bm.SetPixel(X, Y, Color.Silver)
    Next X
    Next Y
    End Sub

    But it flickers REALLY badly. I'm sure there is a better way to do this,
    i just cannot see how :S

    Cheers
  • =?Utf-8?B?VHJhY2tz?=

    #2
    RE: Draw a Grid on a UserControl

    Dont draw it every time in the paint event?

    Just draw the control on form.load one time?

    Or make a flag that only draws it in the paint event when needed.


    "Tom" wrote:
    Hey,
    >
    I have tried drawing a grid onto my control using the following:
    >
    >
    Private Sub VGUI_Grid(...) Handles MyBase.Paint
    Dim new_bitmap As Bitmap
    >
    ' Make a new bitmap that fits the PictureBox.
    new_bitmap = New Bitmap(Me.Width , Me.Height)
    m_BufferGraphic s = Graphics.FromIm age(new_bitmap)
    >
    ' Clear the new bitmap.
    m_BufferGraphic s.Clear(Me.Back Color)
    >
    ' Draw the positioning grid.
    DrawGrid(new_bi tmap)
    >
    ' Copy the existing bitmap's contents into
    ' the new bitmap.
    If Not (m_BufferBitmap Is Nothing) Then
    m_BufferGraphic s.DrawImage(m_B ufferBitmap, 0, 0)
    End If
    >
    ' Save the new bitmap and graphics objects.
    m_BufferBitmap = new_bitmap
    Me.BackgroundIm age = m_BufferBitmap
    End Sub
    >
    >
    Private Sub DrawGrid(ByVal bm As Bitmap)
    For Y As Integer = 0 To bm.Height - 1 Step GRID_SPACING
    For X As Integer = 0 To bm.Width - 1 Step _
    GRID_SPACING
    bm.SetPixel(X, Y, Color.Silver)
    Next X
    Next Y
    End Sub
    >
    But it flickers REALLY badly. I'm sure there is a better way to do this,
    i just cannot see how :S
    >
    Cheers
    >

    Comment

    Working...