Recipe 9.5. Creating a Bitmap


Problem

You want to create off-screen bitmaps to store graphics in memory.

Solution

Sample code folder: Chapter 09\BitmapObject

Create Bitmap objects, and load images into them or draw directly on them.

Discussion

You can create a bitmap in memory, draw graphics onto a Graphics object created for the bitmap, and then draw the bitmap to a form, panel, or other paintable surface. This can provide an increase in speed, and sequentially drawing multiple bitmaps onto a visible surface gives you a simple but effective type of animation.

The code example in this recipe creates a bitmap based on the size of the form and the nature of the Graphics object for the form. A new Graphics object is created based on the new bitmap, so graphics methods will apply to the bitmap. Much of the rest of the code creates radial lines emanating from two points near the center of the bitmap. Finally, once the bitmap graphics are complete, the bitmap is drawn to the form's Graphics object, which paints onto the face of the form:

 Private Sub Form1_Paint(ByVal sender As Object, _       ByVal e As System.Windows.Forms.PaintEventArgs) _       Handles Me.Paint    ' ----- Draw to the form indirectly through a bitmap.    Dim x As Single    Dim y As Single    Dim xc As Single    Dim yc As Single    Dim angle As Single    Dim radians As Single    Dim workImage As Bitmap    Dim canvas As Graphics    ' ----- Create a bitmap that is the same size and    '       format as the form surface.    workImage = New Bitmap(Me.Size.Width, Me.Size.Height, _       e.Graphics)    ' ----- Create a canvas for the bitmap. Drawing on the    '       canvas impacts the bitmap directly.    canvas = Graphics.FromImage(workImage)    ' ---- Draw a radial pattern.    For angle = 0 To 360 Step 2       radians = angle * Math.PI / 180       x = 500 * Math.Cos(radians)       y = 500 * Math.Sin(radians)       yc = Me.ClientSize.Height / 2       xc = Me.ClientSize.Width * 10 / 21       canvas.DrawLine(Pens.Black, xc, yc, xc + x, yc + y)       xc = Me.ClientSize.Width * 11 / 21       canvas.DrawLine(Pens.Black, xc, yc, xc + x, yc + y)    Next angle    ' ----- Stamp the bitmap on the form surface.    e.Graphics.DrawImage(workImage, 0, 0) End Sub 

The key lines of code here are the ones that create the workImage and canvas objects. They create a bitmap compatible with the form and a graphics surface for the bit-map. All drawing methods require a Graphics object to provide a drawing surface. The last line uses the Graphics. DrawImage() method to draw the custom image onto the form, providing a way to get the in-memory bitmap onto a visible surface.

Figure 9-8 shows the new bitmap's contents as drawn onto the face of the form.

Figure 9-8. Drawing an in-memory bitmap onto a form


As you resize this form, its Paint event fires repeatedly, and the bitmap is recreated on the fly. However, it doesn't redraw the entire surface, because Windows tries to limit screen redraws to only those parts that it thinks have changed. In this case, only the newly exposed areas of the form are redrawn. To circumvent this, add the following code to the form:

 Private Sub Form1_Resize(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles Me.Resize    ' ----- Redraw the surface cleanly.    Me.Invalidate() End Sub 

Now the entire image is redrawn as the form size changes.

For the smoothest action be sure to set the form's DoubleBuffered property to true. The combination of double buffering and drawing the lines in-memory on a bitmap creates surprisingly smooth graphics updates as the form is resized.





Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net