Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
Go Back  Xtreme Visual Basic Talk > > > Preventing erasing of drawing when window is painted


Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2009, 07:33 PM
Addy771 Addy771 is offline
Newcomer
 
Join Date: Jan 2009
Posts: 1
Default Preventing erasing of drawing when window is painted


Hello. I'm having a problem with the graphics portion of vb.net. As a school assignment, we are making "turtle graphics" applications. I have mine working perfectly, the problem that we are having (not even the teacher can fix it) is that when the window gets sent the command to be painted (ie minimized and maximized) the drawings in the picturebox I am using are completely erased. I am looking for a way to prevent this from happening. I have been trying to store the image as a bitmap in memory, which does not work. Is there another way to draw to a bitmap/image in memory and then output that to a picturebox?

Any help is appreciated, thanks in advance.
Reply With Quote
  #2  
Old 01-07-2009, 10:53 PM
jo0lsPreventing erasing of drawing when window is painted jo0ls is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2005
Location: London
Posts: 1,050
Default

Short answer: do all your painting inside the Paint event for the form, using the Graphics object that you find inside the PaintEventArgs.

See: Bob Powel's faq.

Code:
Public Class Form1

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.Clear(Color.AliceBlue)
        e.Graphics.DrawLine(Pens.Red, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
        e.Graphics.DrawLine(Pens.Red, 0, Me.ClientSize.Height, Me.ClientSize.Width, 0)
        e.Graphics.DrawEllipse(Pens.Blue, Me.ClientRectangle)
    End Sub

End Class
If you need to trigger the paint event to refresh the control then call the Refresh method. To reduce flicker set the DoubleBuffered property of the form to True.

e.g.

Code:
Public Class Form1

    Private points As List(Of Point)
    Private rand As New Random

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.ClientSize = New Size(600, 600)
        Me.DoubleBuffered = True ' reduces form flickering.
    End Sub

    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        ' Click to refresh the drawing.
        Me.Refresh() ' Triggers the paint event.
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        ' You might need to repaint when the form is resized.
        CreatePoints()
        Me.Refresh()
    End Sub

    Private Sub CreatePoints()
        ' Create 20 points on the circumference of a circle.
        points = New List(Of Point)
        Dim radius As Integer = 250
        Dim midPointX As Integer = Me.ClientSize.Width \ 2
        Dim midPointY As Integer = Me.ClientSize.Height \ 2

        For angle As Single = 0 To 2 * Math.PI Step Math.PI / 20
            Dim x As Double = Math.Cos(angle) * radius
            Dim y As Double = Math.Sin(angle) * radius
            points.Add(New Point(midPointX + CType(x, Integer), midPointY + CType(y, Integer)))
        Next
    End Sub

    Private Function RandomPen() As Pen
        ' To get a random color, use a random argb value.
        ' To get a random solid colour, use a randomm argb value, but make sure the Alpha is 255.
        ' This can be done using a Bitwise Or:
        Dim rgb As Integer = rand.Next Or &HFF000000
        Return New Pen(Color.FromArgb(rgb))
    End Function

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        ' Check the list is initialised:
        If points IsNot Nothing Then
            ' connect each point to all the others
            For first As Integer = 0 To points.Count - 2
                Dim p As Pen = RandomPen()
                For second As Integer = first To points.Count - 1
                    e.Graphics.DrawLine(p, points(first), points(second))
                Next
                p.Dispose() ' This is required to ensure the pen is removed from memory promptly.
            Next
        End If
    End Sub

End Class
Reply With Quote
  #3  
Old 01-08-2009, 07:57 AM
AtmaWeapon's Avatar
AtmaWeaponPreventing erasing of drawing when window is painted AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Supplemental answer:

Windows Forms draws in what is known as an immediate mode. This means that whatever you tell it to draw is immediately drawn, and no information is retained about what has been drawn. This "no information is retained" part is important; it means if you want something to live longer than one draw cycle, you need to tell Windows to draw it during every draw cycle. This means you need to store information about what needs to be drawn, and in the Paint event handler draw this every time the form is repainted.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
 
Preventing erasing of drawing when window is painted
Preventing erasing of drawing when window is painted
 
-->