C # painting brush and fill shape

Source: Internet
Author: User

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:

 
 
  1. SolidBrush mySolidBrush = new SolidBrush(Color.Red);  
  2. 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.

 
 
  1. HatchBrush myHatchBrush =   
  2.    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.

 
 
  1. Graphics MyGraphics = label1.CreateGraphics ();
  2. // Fill the image with a shadow paint brush
  3. HatchBrush mybrush1 = new HatchBrush (
  4. HatchStyle. HorizontalBrick,
  5. Color. Red,
  6. Color. Yellow );
  7. MyGraphics. FillEllipse (mybrush1, 165, 5,170,170 );

The running result is 8-14.

 
Figure 8-14 fill chart

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.

 
 
  1. Image myImage = Image.FromFile("MyTexture.bmp");  
  2. TextureBrush myTextureBrush = new TextureBrush(myImage);  
  3. myGraphics.FillEllipse(myTextureBrush, 0, 0, 100, 50); 

[Example 8-10] fill the image with a texture.

 
 
  1. Graphics myGraphics = label1.CreateGraphics ();
  2. Pen pen = new Pen (Color. Red, 3 );
  3. MyGraphics. DrawEllipse (pen, 25, 10,260,150 );
  4.  
  5. // Create an image object
  6. Image myImage = Image. FromFile ("img.gif ");
  7. // Create a texture image brush
  8. TextureBrush myTextureBrush = new TextureBrush (myImage );
  9. // Fill the elliptical image with a texture brush
  10. 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.

 
 
  1. Graphics myGraphics = label1.CreateGraphics ();
  2. Pen pen = new Pen (Color. Red, 3 );
  3. MyGraphics. DrawEllipse (pen, 25, 10,260,150 );
  4.  
  5. Rectangle myRectangle = new Rectangle (25, 10,260,150 );
  6. // Create a gradient Paint Brush. The color level changes from left to right from blue to green.
  7. LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush (
  8. MyRectangle,
  9. Color. Blue,
  10. Color. Green,
  11. LinearGradientMode. Horizontal );
  12. // Fill the ellipse with a gradient Paint Brush
  13. MyGraphics. FillEllipse (myLinearGradientBrush, myRectangle );

The running result is 8-16.

 
Figure 8-16 fill the image with a gradient Paint Brush

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.