VB 2008 Tutorial - GDI+ Brushes

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
2 posts Page 1 of 1
Contributors
User avatar
tvs praveen
Top Poster
Top Poster
Posts: 205
Joined: Tue Dec 08, 2009 6:20 am

VB 2008 Tutorial - GDI+ Brushes
tvs praveen
Hi, Now i`m gonna show the amazing things which can do with GDI+ Brushes with simple coding stuffs!

The GDI+ Brush object is used to fill areas with colour. Ahh if only life were that simple. Like their cousins Pens, Brushes have some pretty complex behaviours that will require more than a simple introduction. In this article you'll see some of the simpler functions of the brush object and explore a few of the possibilities they offer. In a later article you'll see some of the really neat stuff they can do.

Hot tip: Just as Pens have a set of stock objects, so do Brushes. The Brushes collection contains one solid brush for each of the standard system colours. You can access them by using say, Brushes.Red.

The SolidBrush.

The Brush base-class is abstract. That is to say that it cannot be used as-is because it lacks some implementation details although the way the class interfaces work has been defined. Concrete examples of brushes that do work have been derived from the Brush base class and the simplest of these is the SolidBrush object.

As it's name suggests, this object is used to paint areas with solid colour and is quite simple to use. The initialization is easy, all you have to do is pick a colour. The following listing illustrates this. :)
Code: Select all
Dim sb As New SolidBrush(Color.Blue)
Figure 1 [Screenshot of shapes printed by GDI+ Brushes]

Image

To make this solid GDI+ Brushes in shapes ;)

Just copy and paste this code below Public Class Form1 wahooo;
Code: Select all
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim sb As New SolidBrush(Color.Blue)
        e.Graphics.FillRectangle(sb, 10, 10, 200, 150)
        e.Graphics.FillEllipse(sb, 220, 10, 200, 150)
        e.Graphics.FillPolygon(sb, New Point() {New Point(110, 160), New Point(10, 310), _
        New Point(220, 310)})
        Dim p As New Pen(sb, 10)
        e.Graphics.DrawLines(p, New Point() {New Point(360, 160), _
        New Point(230, 310), _
        New Point(430, 310)})
        p.Dispose()
        sb.Dispose()
        MyBase.OnPaint(e)
    End Sub 'OnPaint
An interesting point to note is that the brush can be used to fill the outline generated by a Pen object. ;)

Down the hatch.

Another brush type derived from Brush is the HatchBrush. This brush enables you to fill shapes with one of the standard hatch styles provided by the system. Hatch patterns are repeating pictures, 8 by 8 pixels square, that tessellate to cover a plane in a seamless texture. Many of the patterns represent brick or trellis-work and can be used to provide something other than a plain field to look at. The HatchBrush may be constructed with one of the hatch-patterns shown in Figure 2. :D

Figure 2 [Screenshot of types of the GDI+ Brushes]

Image

To use a hatch brush in your code you just need to create the brush and choose the colours with which the foreground and background will be painted. In Figure 2 the backgrounds are white and the foregrounds are black but you can of course chose any colour combination. Figure 3 shows the identical code used to draw the SolidBrush example but this time a HatchBrush will be used to fill the shapes instead.

In this instance, the brush uses the DiagonalBrick hatch style and employs Brown and Gray for the background and foreground colours. The following listing shows the code that drew this image.

Figure 2 [Screenshot of GDI+ shape painted by HatchBrush]

To make this shapes with HatchBrush just copy and paste this code and debug, You will get amazing shapes painted by HatchBrush

You can also change sizes of each shapes, colours and also can paint in anyshapes using this code

First you add this Imports above the Public Class Form1
Code: Select all
Imports System.Drawing.Drawing2D
Then paste this code below Public Class Form1
Code: Select all
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim hb As New HatchBrush(HatchStyle.DiagonalBrick, Color.Gray, Color.Brown)
        e.Graphics.FillRectangle(hb, 10, 10, 200, 150)
        e.Graphics.FillEllipse(hb, 220, 10, 200, 150)
        e.Graphics.FillPolygon(hb, New Point() {New Point(110, 160), _
                                                New Point(10, 310), _
                                                New Point(220, 310)})
        Dim p As New Pen(hb, 10)
        e.Graphics.DrawLines(p, New Point() {New Point(360, 160), _
                                             New Point(230, 310), _
                                             New Point(430, 310)})
        p.Dispose()
        hb.Dispose()
        MyBase.OnPaint(e)
    End Sub 'OnPaint
Painting with pictures.

The last type of brush for this article is the TextureBrush. This object is once-again derived from the abstract Brush class and enables you to fill an area with the contents of an image. This is particularly useful if you can obtain a seamlessly tiling texture such as a marble or brickwork image. The texture shown in Figure 4 was generated by Sausage Software's Reptile program and will tile to cover any area.

Figure 4 [Screenshot of a GDI+ shape painted with picture]

Image

The TextureBrush requires an image at construction which you can load from disk like this...
Code: Select all
Dim img As Image=Image.FromFile("imagefile.bmp")
Dim tb As New TextureBrush(img)
This brush may be used to draw the same set of figures but this time with an image instead of a colour or a hatch pattern. Figure 5 shows the application in action.

Figure 5 [Screenshot of GDI+ Brush painted shapes with picture]

Image


In this instance, the brush uses the DiagonalBrick hatch style and employs Brown and Gray for the background and foreground colours. The following listing shows the code that drew this image.


*******************Thanks for reading my tutorial, I think this Tutorial will be most helpful to you!*******************

If you want any custom tutorial for you or you need any help in VS.NET, VB.NET, Software Coding, Designing and much more any help in Computer stuffs just ask me

Mostly i will help everyone in coding and design stuffs in on Computer

- Best regards hehaho;

- Tvs Praveen wahooo;

- Thanks CodeNStuff! for this amazing Website cooll;
Lewis
Coding God
Coding God
Posts: 1564
Joined: Sun Dec 20, 2009 2:12 pm

Re: VB 2008 Tutorial - GDI+ Brushes
Lewis
These tutorials are absolutly amazing!
Image
2 posts Page 1 of 1
Return to “Tutorials”