View Single Post
 
Old 03-17-2004, 10:50 PM
MikeJ's Avatar
MikeJ MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,747
Default GDI+ in .Net - Part 3: Bitmaps and TextureBrushes

GDI+ in .Net - Part 3: Bitmaps and TextureBrushes

What are these pesky bitmaps? MSDN says it "encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes." Now, if you are like me, you might be thinking, huh? Well, basically this means that you can store pixel data onto it. (And, by association, you could call a Bitmap a raster image.)
Now, how do we make a bitmap? It is rather simple, it's like any other variable:

Code:
Dim bBackground As Bitmap = New Bitmap(Application.StartupPath & "\background.bmp")
Woah! What did we just do? We created a new bitmap (called bBackground) and set its content from the file background.bmp which can be found in the startup directory for our application. Now that we have our background Bitmap, what can we do with it? We can use the TextureBrush to apply a texture to our form!

Before we do that though, we will need to create a TextureBrush. Remember from Part 1 that SolidBrushes will let you create a brush that is a solid color. On the other hand, TextureBrushes let you create a brush that uses a Bitmap or an Image to fill something created by the graphics object. In the following example, I'll use the bBackground bitmap that we have already created to create a TextureBrush that contains background.bmp.

Code:
Dim bBGBrush As TextureBrush = New TextureBrush(bBackground)
Now we have our TextureBrush that is made from background.bmp, we can create a background texture for our form.

[ MSDN article on Texture Brushes ]



Creating a Texturized Form
In order to create a texturized form, we will need a rectangle that is the entire size of the form.
Code:
Dim bgRECT As Rectangle = New Rectangle(0, 0, Me.Width, Me.Height)
As you remember from Part 1, this creates a rectangle that will fill the whole form. Now, we can use the FillRectangle (see Part 1) method to fill the form.
Code:
g.FillRectangle(bBGBrush, bgRECT)
Here is the complete project:
Code:
Imports System.Drawing

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        'Declare the graphics object
        Dim g As Graphics = e.Graphics

        'Declare the background object
        Dim bBackground As Bitmap = New Bitmap(Application.StartupPath & "\background.bmp")

        'Declare the brush
        Dim bBGBrush As TextureBrush = New TextureBrush(bBackground)

        'Declare the rectangle
        Dim bgRECT As Rectangle = New Rectangle(0, 0, Me.Width, Me.Height)

        'Fill bgRECT
        g.FillRectangle(bBGBrush, bgRECT)
    End Sub
End Class
This seems really simple, but can produce stunning effects when you have a good background texture and apply it to the form using this method. Also, keep watching, as the next addition to this ongoing series will be on the Gradient Brushes.

P.S.
I've included in the ZIP attachment a sample background.bmp.
Attached Files
File Type: zip GDI_TextureBrushes.zip (6.6 KB, 197 views)
__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!

Last edited by MikeJ; 03-17-2004 at 11:04 PM.
Reply With Quote