C # simple drawing example (suitable for beginners)
If there is reprint, please indicate the source: http://www.cnblogs.com/flydoos/archive/2011/09/22/2184943.html
C # simple drawing example (suitable for beginners)
It is specially designed for beginners, and some user-friendly comments are added for beginners to understand. Good master.
/* Title: C # GDI + simple drawing example
* Author: flydoos
* QQ group: C #/Java technology exchange group (96020642)
* Blog: Http://flydoos.cnblogs.com
* Weibo: Http://weibo.com/flydoos
* Date: 2011-09-22
*
* Because they are very similar, the rest will not be written.
* For example, draw a filled polygon drawpolygon and fillpolygon.
* Read the prompt and write it.
*
*/
UsingSystem;
UsingSystem. drawing;
UsingSystem. Windows. forms;
NamespaceGdi_demo
{
Public Partial ClassMainform: Form
{
PublicMainform ()
{
Initializecomponent ();
}
// Draw a straight line
Private Void Btndrawline_click ( Object Sender, eventargs E)
{
Graphics G = This . Creategraphics ();
Color red = color. fromargb ( 255 , 0 , 0 );
Pen blackpen = New Pen (red, 3 );
Point point1 = New Point ( 100 , 100 ); // Coordinates (100,100)
Point point2 = New Point ( 500 , 100 ); // Coordinates (500,100)
G. drawline (blackpen, point1, point2 ); // Connect two coordinates to a straight line
}
// Force object re-painting
Private Void Btninvalidate_click ( Object Sender, eventargs E)
{
Rectangle r = New Rectangle ( 10 , 10 , 200 , 400 ); // Defines a rectangular area with a width of 200 and a height of 200 starting from coordinates ()
This . Invalidate (R );
}
//Clear the entire drawing screen and fill it with a certain color
Private VoidBtnclear_click (ObjectSender, eventargs E)
{
Graphics G =This. Creategraphics ();
G. Clear (color. Red );
}
// Draw an arc
Private Void Btndrawarc_click ( Object Sender, eventargs E)
{
Graphics G = This . Creategraphics ();
Rectangle r = New Rectangle ( 10 ,10 , 200 , 100 );
Pen pen1 = New Pen (color. Black );
G. drawarc (pen1, 50 , 50 , 200 , 200 , 180 , 90 ); // Starting coordinate (50, 50), width 200, height 200, arc starting angle 180 °, arc passing angle 90 °
G. drawarc (pen1, R, 0 , 135 );
}
// Draw an ellipse and fill an ellipse
Private Void Btndrawellipse_click ( Object Sender, eventargs E)
{
Graphics G = This . Creategraphics ();
Rectangle r = New Rectangle ( 10 , 10 , 200 , 100 );
Pen pen1 = New Pen (color. Black );
// Elliptic
G. drawellipse (pen1, 50 , 50 , 200 , 200 ); // Start coordinate (50, 50), width 200, height 200
G. drawellipse (pen1, R );
// Fill with the elliptic
G. fillellipse ( New Solidbrush (color. Blue ), 50 , 50 , 200 , 200 );
G. fillellipse ( New Solidbrush (color. darkorange), R );
}
// Draw and fill a rectangle
Private Void Btnrectangle_click ( Object Sender, eventargs E)
{
Graphics G = This . Creategraphics ();
Rectangle r = New Rectangle ( 10 , 10 , 200 , 100 );
Pen pen1 = New Pen (color. Black );
// Rectangle
G. drawrectangle (pen1, 50 , 50 , 200 , 200 ); // Start coordinate (50, 50), width 200, height 200
G. drawrectangle (pen1, R );
// Fill rectangle
G. fillrectangle ( New Solidbrush (color. Blue ), 50 , 50 , 200 , 200 );
G. fillrectangle ( New Solidbrush (color. darkorange), R );
}
Private Void Btndrawpie_click ( Object Sender, eventargs E)
{
Graphics G = This . Creategraphics ();
Rectangle r = New Rectangle ( 10 , 10 , 200 , 100 );
Pen pen1 = New Pen (color. Black );
// Elliptic
G. drawpie (pen1, 50 , 50 ,200 , 200 , 225 , 90 ); // Start coordinate (50, 50), width 200, height 200, Ray 1 = 225, Ray 2 = 90
G. drawpie (pen1, R, 225 , 90 );
// Fill with the elliptic
G. fillpie ( New Solidbrush (color. Blue ), 50 , 50 , 200 , 200 , 225 , 90 );
G. fillpie ( New Solidbrush (color. darkorange), R, 225 , 90 );
}
}
}
C # GDI + simple drawing example source code (vs2010 is suitable for beginners)
: Network Disk download 1 backup download 2
If you have reproduced, please indicate the source: http://www.cnblogs.com/flydoos/archive/2011/09/22/2184943.html special attention: if you are not vs2010, then please download here [Visual Studio version Conversion Tool], after conversion, you can use 2005 and 2008 to open something 2010 ....
Download:Http://www.cnblogs.com/flydoos/archive/2011/09/19/2181106.html