有關圖形方面的一篇文章,不妨一看

來源:互聯網
上載者:User
圖形 Working with Brushes in GDI+ Drawing GDI+ Objects
The following code draws a line, an ellipse, a curve, and a polygon object. As you can see from the code, I抳e used pen object to fill these objects. See more details.

protected override void OnPaint(PaintEventArgs e)
{
      Graphics g = e.Graphics;
      Pen pn = new Pen(Color.Green, 10);
      g.DrawLine(pn, 100, 10, 30, 10);
      g.DrawEllipse( new Pen(Color.Red, 20), 20, 40, 20, 20);
      g.DrawBezier( new Pen(Color.Blue, 5), new Point(50,60), new      
Point(150,10), new Point(200,230), new Point(100,100) );          
       PointF point1 = new PointF(50.0f, 250.0f);                  
      PointF point2 = new PointF(100.0f, 25.0f);
      PointF point3 = new PointF(150.0f, 5.0f);
      PointF point4 = new PointF(250.0f, 50.0f);
      PointF point5 = new PointF(300.0f, 100.0f);

     PointF[] curvePoints = {point1, point2, point3, point4, point5 };
      g.DrawPolygon(new Pen(Color.Chocolate, 10), curvePoints);
}
  Brush and Brushes Types  
Brush type is an abstract base class. HatchBrush, LinearGradientBrush, PathGradientBrush, SolidBrush and TextureBrush classes are inherited from Brush class. You don抰 use this class directly.
All brush types are defined in System.Drawing and its helper namespaces. Before using brushes, you need to add reference to this namespace. HatchBrush and GradientBrush are defined in System.Drawing.Drawing2D namespace.
You use brushes to fill GDI+ objects with certain kind of brush. You generally call Fill methods of Graphics class to fill various objects such as Ellipse, Arc, or Polygon. There are different kinds of brushes. For example, solid brush, hatch brush, texture brush, and gradient brush.Solid Brushes
Solid brushes are normal brushes with no style. You fill GDI+ object with a color. SolidBrush type is used to work with solid brushes.

Graphics g = e.Graphics;                                    
SolidBrush sdBrush1 = new SolidBrush(Color.Red);                  
SolidBrush sdBrush2 = new SolidBrush(Color.Green);         
SolidBrush sdBrush3 = new SolidBrush(Color.Blue);    
g.FillEllipse(sdBrush2, 20, 40, 60, 70);              
Rectangle rect = new Rectangle(0, 0, 200, 100);            
g.FillPie(sdBrush3, 0, 0, 200, 40, 0.0f, 30.0f );                 
PointF point1 = new PointF(50.0f, 250.0f);                       
PointF point2 = new PointF(100.0f, 25.0f);                  
PointF point3 = new PointF(150.0f, 40.0f);                  
PointF point4 = new PointF(250.0f, 50.0f);                  
PointF point5 = new PointF(300.0f, 100.0f);                             
PointF[] curvePoints = {point1, point2, point3, point4, point5 };    g.FillPolygon(sdBrush1, curvePoints);
The following code draws an ellipse, a pie, and a polygon. Hatch Brushes
using System.Drawing.Drawing2D;
The hatch brushes are brushes with a hatch style, a foreground color, and a background color. Hatches are a combination of rectangle lines and the area between the lines. The foreground color defines the color of lines; the background color defines the color of area between lines.
HatchStyle defines the hatch styles.

Member Name
BackwardDiagonal
Cross
DarkDownwardDiagonal
DarkHorizontal
DarkUpwardDiagonal
DarkVertical
DashedDownwardDiagonal
DashedHorizontal
DashedUpwardDiagonal
DashedVertical
DiagonalBrick
DiagonalCross
Divot
DottedDiamond
DottedGrid
ForwardDiagonal
The following code shows how to draw hatch brushes.


protected override void OnPaint(PaintEventArgs e)
{
      Graphics g = e.Graphics;
      HatchBrush hBrush1 = new HatchBrush(HatchStyle.DiagonalCross, Color.Chocolate, Color.Red);
      HatchBrush hBrush2 = new HatchBrush(HatchStyle.DashedHorizontal, Color.Green, Color.Black);
      HatchBrush hBrush3 = new HatchBrush(HatchStyle.Weave, Color.BlueViolet, Color.Blue);
      g.FillEllipse(hBrush1, 20, 80, 60, 20);
      Rectangle rect = new Rectangle(0, 0, 200, 100);
      g.FillPie(hBrush3, 0, 0, 200, 40, 0.0f, 30.0f );
      PointF point1 = new PointF(50.0f, 250.0f);                  
      PointF point2 = new PointF(100.0f, 25.0f);
      PointF point3 = new PointF(150.0f, 40.0f);
      PointF point4 = new PointF(250.0f, 50.0f);
      PointF point5 = new PointF(300.0f, 100.0f);
  
      PointF[] curvePoints = {point1, point2, point3, point4, point5 };
      g.FillPolygon(hBrush2, curvePoints);
}
  
And the result looks like following -
Texture Brushes
The texture brushes provides you to use an image as brush and fill GDI+ objects with the brush. The following code use 搈yfile.bmp?as a brush. You need to define an Image object and create brush with that Image and pass the brush into Fill method of GDI+ objects.

private Brush txBrush;

protected override void OnPaint(PaintEventArgs e)
{
      Graphics g = e.Graphics;      
      g.FillRectangle(txBrush, ClientRectangle);
}
private void Form1_Load(object sender, System.EventArgs e)
{
      Image img = new Bitmap(@"C:\myfile.bmp");
      txBrush = new TextureBrush(img);
}
The result looks like following:
Gradient Brushes
Gradient brushes are provides more color to your GDI+ objects. By using LinearGradientBrush type, you can blend two colors together. The following code blends red and green colors.

protected override void OnPaint(PaintEventArgs e)
{
      Graphics g = e.Graphics ;   
      Rectangle rect = new Rectangle(50, 30, 200, 200);   
      LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Green,LinearGradientMode.BackwardDiagonal);  
      g.FillRectangle(lBrush, rect);
}
And the result looks like the following:
This like combines blue and green colors -   

LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Blue, Color.Green, LinearGradientMode.Vertical);
And the result looks like following:  


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。