Http://book.51cto.com/art/200908/148535.htm
A closed shape (such as a rectangle or an ellipse) consists of a contour and an interior. Draw the outline with a pen and fill the interior with a paint brush. GDI + provides several paint brush classes for filling in closed shapes: SolidBrush, HatchBrush, TextureBrush, and GradientBrush. All these classes are inherited from the Brush class. Figure 8-12 shows the ellipse filled with a solid paint brush and the rectangle filled with a shadow paint brush.
|
(Click to view the larger image) Figure 8-12 filled Image |
1. Use a solid paint brush
To fill a closed image, you must have a Graphics object and a Brush object. The Graphics object provides methods such as FillRectangle and FillEllipse. The Brush object stores fill attributes, such as colors and patterns. The Brush object is passed to the fill method as one of the parameters. For example, fill the ellipse with pure red:
- SolidBrush mySolidBrush = new SolidBrush(Color.Red);
- myGraphics.FillEllipse(mySolidBrush, 0, 0, 60, 40);
Note that in the previous example, the paint Brush is of the SolidBrush type inherited from the Brush.
2. Use shadow paint brush
You must specify the foreground color, background color, and shadow style when filling the image with a shadow paint brush. The foreground color is the shadow color.
- HatchBrush myHatchBrush =
- new HatchBrush(HatchStyle.Vertical, Color.Blue, Color.Green);
GDI + provides more than 50 shadow styles. The three styles shown in 8-13 are horizontal, diagonal, and cross.
|
(Click to view the larger image) Figure 8-13 shadow Style |
In the following example, a circle is drawn, which is filled with a cross shadow paint brush.
[Example 8-9] draw a fill image.
- Graphics MyGraphics = label1.CreateGraphics ();
- // Fill the image with a shadow paint brush
- HatchBrush mybrush1 = new HatchBrush (
- HatchStyle. HorizontalBrick,
- Color. Red,
- Color. Yellow );
- MyGraphics. FillEllipse (mybrush1, 165, 5,170,170 );
The running result is 8-14.
3. Use texture painter
With the texture paint brush, you can fill the image with the pattern stored in the bitmap. For example, assume that an image named mytexture.bmp is stored in an image. The following code uses an image in mytexture.bmp to fill the ellipse.
- Image myImage = Image.FromFile("MyTexture.bmp");
- TextureBrush myTextureBrush = new TextureBrush(myImage);
- myGraphics.FillEllipse(myTextureBrush, 0, 0, 100, 50);
[Example 8-10] fill the image with a texture.
- Graphics myGraphics = label1.CreateGraphics ();
- Pen pen = new Pen (Color. Red, 3 );
- MyGraphics. DrawEllipse (pen, 25, 10,260,150 );
-
- // Create an image object
- Image myImage = Image. FromFile ("img.gif ");
- // Create a texture image brush
- TextureBrush myTextureBrush = new TextureBrush (myImage );
- // Fill the elliptical image with a texture brush
- MyGraphics. FillEllipse (myTextureBrush, 25, 10,260,150 );
The running result is 8-15.
|
Figure 8-15 texture Filling |
4. Use a gradient Paint Brush
GDI + provides two gradient paint brushes: Linear and path. You can use linear gradient paint brushes to fill the image with colors that will gradually change when the image is moved horizontally, vertically, or diagonally.
The following example uses a horizontal gradient Paint Brush to fill an oval. when moving from the left edge of the oval to the right edge, the color of the paint brush changes from blue to green.
[Example 8-11] fill the image with a gradient paint brush.
- Graphics myGraphics = label1.CreateGraphics ();
- Pen pen = new Pen (Color. Red, 3 );
- MyGraphics. DrawEllipse (pen, 25, 10,260,150 );
-
- Rectangle myRectangle = new Rectangle (25, 10,260,150 );
- // Create a gradient Paint Brush. The color level changes from left to right from blue to green.
- LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush (
- MyRectangle,
- Color. Blue,
- Color. Green,
- LinearGradientMode. Horizontal );
- // Fill the ellipse with a gradient Paint Brush
- MyGraphics. FillEllipse (myLinearGradientBrush, myRectangle );
The running result is 8-16.
|
Figure 8-16 fill the image with a gradient Paint Brush |