rbulph Posted November 7, 2005 Posted November 7, 2005 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? Quote
Leaders snarfblam Posted November 7, 2005 Leaders Posted November 7, 2005 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. Quote [sIGPIC]e[/sIGPIC]
rbulph Posted November 8, 2005 Author Posted November 8, 2005 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 Quote
Leaders snarfblam Posted November 8, 2005 Leaders Posted November 8, 2005 Use the Graphics.FromImage method to create a graphics object which can manipulate a bitmap. Quote [sIGPIC]e[/sIGPIC]
rbulph Posted November 8, 2005 Author Posted November 8, 2005 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? Quote
jo0ls Posted November 8, 2005 Posted November 8, 2005 Dim g as Graphics = Graphics.FromImage(bitmapName) Quote
Leaders snarfblam Posted November 9, 2005 Leaders Posted November 9, 2005 [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. Quote [sIGPIC]e[/sIGPIC]
rbulph Posted November 9, 2005 Author Posted November 9, 2005 [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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.