GDI + draw and brush fill graphics

Source: Internet
Author: User

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

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.