首页 > 开发 > 综合 > 正文

有关图形方面的一篇文章,不妨一看

2024-07-21 02:23:54
字体:
来源:转载
供稿:网友
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:  
菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表