Drawing Graphics with System.Drawing namespace in C# and .NET Framework
Class |
---|
Bitmap |
Brush |
Brushes |
ColorConverter |
ColorTranslator |
Font |
FontConverter |
FontFamily |
Graphics |
Icon |
IconConverter |
Image |
ImageAnimator |
ImageConverter |
ImageFormatConverter |
Pen |
Pens |
PointConverter |
RectangleConverter |
Region |
SizeConverter |
SolidBrush |
StringFormat |
SystemBrushes |
SystemColors |
SystemFonts |
SystemIcons |
SystemPens |
TextureBrush |
ToolboxBitmapAttribute |
Structures
Class |
---|
CharacterRange |
Color |
Point |
PointF |
Rectangle |
RectangleF |
Size |
SizeF |
Point and Size and Colors are used to position form elements within the form. It’s used in many different ways. Here are several examples how it is done.
button1.Location = new Point(10, 10);
button1.Left = 10;
button1.Top = 10;
button1.Size = new Size(30, 30);
button1.ForeColor = Color.Red;
button1.BackColor = Color.Blue; custom button1.ForeColor = Color.FromArgb(10, 200, 200);
In order to draw line you need to call Graphics object and then you’ll have several methods readily available to you such as:Clear, DrawEllipse, DrawIcon, DrawLine, DrawLines, DrawPath, DrawPie, DrawPolygon, DrawRectangle, DrawRectangles, DrawString in order to use this methods you must provide instance for the Pen class.
Graphics myGraphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Red, 7);
myGraphics.DrawLine(myPen, 1, 1, 200, 200);
OR
Graphics myGraphics = this.CreateGraphics();
Pen myPoint = new Pen(Color.MediumPurple, 2);
Point[] points = new Point[]
{new Point(10, 10),
new Point(10, 100),
new Point(20, 65),
new Point(200, 100),
new Point(95, 40)};
myGraphics.DrawPolygon(myPoint, points);
Pens can be customized in variety of different ways for example patterns and endcaps. You can also fill images or shapes using Graphics.Fill method.
Graphics myGraphics = this.CreateGraphics();
Brush myBrash = new SolidBrush(Color.Maroon);
Point[] points = new Point[]
{new Point(10, 10),
new Point(10, 100),
new Point(30, 65),
new Point(200, 100),
new Point(95, 40)};
myGraphics.FillPolygon(myBrash, points);
We can also draw a circle usingGraphics.DrawEllipse method.