Flylib.com

Books Software

 
 
 

10.3 Drawing a Rectangle with a Hatch Brush

 <  Day Day Up  >  

10.3 Drawing a Rectangle with a Hatch Brush

You want to draw a rectangle and fill its interior with a certain pattern .


Technique

The DrawRectangle and FillRectangle method both use a Brush object. The previous recipe used a SolidBrush to fill the border or interior of a rectangle with a single solid color. To draw the border or fill the interior with a specified color and a pattern, use a HatchBrush object. In addition to a background Color value and foreground Color value, the HatchBrush constructor also requires a value from the HatchStyle enumerated data type.

Listing 10.2 Drawing Rectangles with a HatchBrush
private void Form1_Click(object sender, System.EventArgs e)

{

    Random randNum = new Random( DateTime.Now.Millisecond );



    int width = randNum.Next( this.Width-mouseHit.X );

    int height = randNum.Next( this.Height-mouseHit.Y );

    int fr = randNum.Next(255);

    int fg = randNum.Next(255);

    int fb = randNum.Next(255);

    int br = randNum.Next(255);

    int bg = randNum.Next(255);

    int bb = randNum.Next(255);



    Graphics surface = Graphics.FromHwnd(this.Handle);

    surface.FillRectangle( new HatchBrush((HatchStyle)Enum.Parse(

        typeof(HatchStyle), comboBox1.SelectedItem.ToString()),

        Color.FromArgb(fr,fg,fb), Color.FromArgb(br,bg,bb)),

        mouseHit.X, mouseHit.Y, width, height );

}



private void Form1_MouseDown(object sender,

    System.Windows.Forms.MouseEventArgs e)

{

    mouseHit = new Point( e.X, e.Y );

}



private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)

{

    Invalidate();

}

Comments

In the last recipe, you saw how to create a SolidBrush object representing a solid color. You could then use this brush to fill the interior of a shape or use it as the ink for a Pen object. Every shape-rendering method within the Graphics class uses a generic Brush object, but through object-oriented programming principles, you can pass any object that is derived from the Brush class. One of these is the HatchBrush , which renders a solid color that is then overlaid with a pattern rendered in a different color.

There are three main components to a HatchBrush . The BackgroundColor property is the solid color that is displayed in the areas not filled with the pattern. The pattern itself is rendered using the color specified in the ForegroundColor property. Finally, the HatchStyle property defines the pattern and is set to one of the several different values defined in the HatchStyle enumerated data type. In Figure 10.1, you can see different rectangles rendered with different hash styles.

Figure 10.1. You can use a HatchBrush to render patterns within the interior of a shape or as the ink for a Pen that draws an unfilled shape.

graphics/10fig01.jpg

 <  Day Day Up  >  
 <  Day Day Up  >  

10.4 Filling a Rectangle with a Texture Brush

You want to create a rectangle that uses an image to fill its interior.


Technique

The process of drawing a rectangle, or any other shape, using a texture-based brush is similar to those of the last two recipes. Call the DrawRectangle or FillRectangle method using a TextureBrush object as the brush. The TextureBrush constructor uses a single parameter, which is an Image object. Recipe 10.15 details different ways to load an image. The easiest way is to call the static method FromFile defined in the Image class passing the path to an image file on the file system:


Graphics g = panel1.CreateGraphics();

Image img = Image.FromFile( "myimage.jpg" );

g.FillRectangle( new TextureBrush(img), e.X, e.Y, img.Width, img.Height );

g.Dispose();

Comments

You can use a HatchBrush to fill a shape with a specified pattern. However, the HatchBrush class doesn't allow a developer to define his own pattern. You can use the TextureBrush class to create your own pattern, although you lose the benefit of being able to specify a background and foreground color . A TextureBrush uses an Image as its rendering output.

When rendering a shape using a TextureBrush , the image is not aligned with the X and Y coordinate of the shape being drawn. For instance, the top-left corner of the image associated with a TextureBrush is not aligned with the top-left corner of the shape being drawn. In fact, the image is aligned with the corner of the visible surface being rendered to, and if the image is smaller than the width and height of the surface, it is tiled. With that little bit of information, you can see how easy it is to create a Windows Form whose background contains a tiled image:


protected override void OnPaint(PaintEventArgs e)

{

    e.Graphics.FillRectangle( new TextureBrush( myImage ),

        0, 0, this.Width, this.Height );

}
 <  Day Day Up  >