You can use GDI + to draw images programmatically in Windows Forms applications.
You can create a project-Windows Forms Application-create a form in. First introduce the namespace using system. Drawing. imaging; using system. Drawing. drawing2d;
1. Draw a rectangle
You can add the following code to the Form painting event.
Private void form1_paint (Object sender, painteventargs E)
{
// Create pen and dot
Pen redpen = new pen (color. Red, 1 );
Pen bluepen = new pen (color. Blue, 2 );
Pen greenpen = new pen (color. Green, 3 );
Float x = 5.0f, y = 5.0f;
Float width = 100366f;
Float Height = 200366f;
Graphics G = This. creategraphics (); // The creategraphics () method exists only when a form or control exists.
// Create a rectangle
Rectangle rect = new rectangle (20, 20, 80, 40 );
// Draw three rectangles
G. drawrectangle (bluepen, X, Y, width, height );
G. drawrectangle (redpen, 60, 80,140, 50 );
G. drawrectangle (greenpen, rect );
// Release the object
Redpen. Dispose ();
Bluepen. Dispose ();
Greenpen. Dispose ();
G. Dispose ();
}
2. Draw an ellipse
The above two namespaces must also be introduced.
You can pull a button control, draw an ellipse in the button click event, and write the following code.
Graphics G = This. creategraphics ();
Pen p1 = new pen (color. Black );
// Draw an ellipse
G. drawellipse (P1, 50,30, 30,50 );
// Draw a straight line
G. drawline (P1 );
3. Fill images (using painter) there are four types of painter
Soildbrush
Write the following code in the button event:
Graphics G = This. creategraphics ();
Solidbrush FF = new solidbrush (color. Red );
// Fill in the interior of the ellipse // obtain the rectangle representing the workspace of the control
G. fillellipse (FF, clientrectangle );
GDI + draw and brush fill graphics