Persisting Graphics

rbulph

Well-known member
Joined
Feb 17, 2003
Messages
343
Ive looked and Im sure it must be on here somewhere, but I cant find it. How do I persist an image in VB.net? Say I wanted to show an image that joined up all the points that a mouse had moved to. I can draw in the Paint event, but this is the MouseMove event, so how do I handle that? How do I store the previous image before drawing the next line on it?
 
Instead of drawing to the window, you can draw to a bitmap and set the bitmap as the controls Image or BackGroundImage property, calling the Invalidate method to refresh the image.
 
marble_eater said:
Instead of drawing to the window, you can draw to a bitmap and set the bitmap as the controls Image or BackGroundImage property, calling the Invalidate method to refresh the image.

Thanks. So how do I draw to the bitmap? It doesnt seem to have any graphics methods. Heres my code which doesnt work because the best I can do is draw to a Graphics object:


Code:
Public Class Form1
    Dim g As System.Drawing.Graphics
    Dim b As Bitmap

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        g = Me.PictureBox1.CreateGraphics

        b = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height, g)

        Dim rt As New System.Drawing.RectangleF
        Dim pr As New System.Drawing.SizeF
        pr.Width = 20
        pr.Height = 40
        rt.Size = pr

        g.DrawEllipse(Pens.Black, rt)

        Me.PictureBox1.Invalidate()

    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        Me.PictureBox1.Image = b

    End Sub
End Class
 
Use the Graphics.FromImage method to create a graphics object which can manipulate a bitmap.
 
marble_eater said:
Use the Graphics.FromImage method to create a graphics object which can manipulate a bitmap.
Im none the wiser. Where? And with what argument?
 
[Vb]
Create an image to persist
Dim MyBitmap As New Bitmap(MyPictureBox.Width, MyPictureBox.Height)
Persist, Image. Goooood image.
MyPictureBox.Image = MyBitmap
Now we can manipulate Image.
Dim MyGraphics As Graphics = Graphics.FromImage(MyBitmap)
[/VB]
Chances are you will want the Graphics object, and maybe the Bitmap object, to be of class scope.
 
marble_eater said:
[Vb]
Create an image to persist
Dim MyBitmap As New Bitmap(MyPictureBox.Width, MyPictureBox.Height)
Persist, Image. Goooood image.
MyPictureBox.Image = MyBitmap
Now we can manipulate Image.
Dim MyGraphics As Graphics = Graphics.FromImage(MyBitmap)
[/VB]
Chances are you will want the Graphics object, and maybe the Bitmap object, to be of class scope.
OK, thanks, seems to work.

What I was thinking was that I needed to just draw what I was drawing in the MouseMove event and avoid triggering the paint event of the PictureBox, because I thought this would lead to flickering. But your code does trigger that event and there is no flickering. If I use the pictureboxs CreateGraphics method then this seems to work just as well.
 
Back
Top