Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I've looked and I'm sure it must be on here somewhere, but I can't 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?
  • Leaders
Posted
Instead of drawing to the window, you can draw to a bitmap and set the bitmap as the control's Image or BackGroundImage property, calling the Invalidate method to refresh the image.
[sIGPIC]e[/sIGPIC]
Posted
Instead of drawing to the window' date=' you can draw to a bitmap and set the bitmap as the control's Image or BackGroundImage property, calling the Invalidate method to refresh the image.[/quote']

 

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

 

 

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

Posted
Use the Graphics.FromImage method to create a graphics object which can manipulate a bitmap.

I'm none the wiser. Where? And with what argument?

  • Leaders
Posted

[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)

[/code]

Chances are you will want the Graphics object, and maybe the Bitmap object, to be of class scope.

[sIGPIC]e[/sIGPIC]
Posted
[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)

[/code]

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 picturebox's CreateGraphics method then this seems to work just as well.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...