Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Draw Shapes in C#: Lines, Arcs, Ellipses, and Rectangles
// Create BmpOptions
BmpOptions bmpCreateOptions = new BmpOptions();
bmpCreateOptions.BitsPerPixel = 32;
// Define the source property for the instance of BmpOptions
bmpCreateOptions.Source = new StreamSource();
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
Image image = Image.Create(bmpCreateOptions, 500, 500);
// Create and initialize an instance of Graphics class
Graphics graphic = new Graphics(image);
// Clear the image surface with White color
graphic.Clear(Color.White);
// Draw a dotted arc shape by specifying the Pen object having red black
// color and coordinates, height, width, start & end angles
int width = 200;
int height = 300;
int startAngle = 45;
int sweepAngle = 270;
// Draw arc to screen
graphic.DrawArc(new Pen(Color.Black, 3), 0, 0, width, height, startAngle, sweepAngle);
// Save all changes
image.Save("draw_arc.bmp");
// Create BmpOptions
BmpOptions bmpCreateOptions = new BmpOptions();
bmpCreateOptions.BitsPerPixel = 32;
// Define the source property for the instance of BmpOptions
bmpCreateOptions.Source = new StreamSource();
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
Image image = Image.Create(bmpCreateOptions, 500, 500);
// Create and initialize an instance of Graphics class
Graphics graphic = new Graphics(image);
// Clear the image surface with White color
graphic.Clear(Color.White);
// Draw a dotted ellipse shape by specifying the Pen object having red
// color and a surrounding Rectangle
graphic.DrawEllipse(new Pen(Color.Red, 3), new Rectangle(60, 40, 70, 120));
// Draw a continuous ellipse shape by specifying the Pen object having
// solid brush with blue color and a surrounding Rectangle
graphic.DrawEllipse(new Pen(new SolidBrush(Color.Blue), 3),
new Rectangle(40, 60, 120, 70));
// Save all changes
image.Save("draw_ellipse.bmp");
// Create BmpOptions
BmpOptions bmpCreateOptions = new BmpOptions();
bmpCreateOptions.BitsPerPixel = 32;
// Define the source property for the instance of BmpOptions
bmpCreateOptions.Source = new StreamSource();
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
Image image = Image.Create(bmpCreateOptions, 500, 500);
// Create and initialize an instance of Graphics class
Graphics graphic = new Graphics(image);
// Clear the image surface with White color
graphic.Clear(Color.White);
// Draw a dotted line by specifying the Pen object having blue color and
// co-ordinate Points
graphic.DrawLine(new Pen(Color.Blue, 3), 18, 18, 200, 200);
graphic.DrawLine(new Pen(Color.Blue, 3), 18, 200, 200, 18);
// Draw a continuous line by specifying the Pen object having Solid
// Brush with red color and two point structures
graphic.DrawLine(new Pen(new SolidBrush(Color.Red), 3),
new Point(18, 18), new Point(18, 200));
// Draw a continuous line by specifying the Pen object having Solid
// Brush with white color and two point structures
graphic.DrawLine(new Pen(new SolidBrush(Color.Orange), 3),
new Point(200, 18), new Point(18, 18));
// Save all changes
image.Save("draw_lines.bmp");
// Create BmpOptions
BmpOptions bmpCreateOptions = new BmpOptions();
bmpCreateOptions.BitsPerPixel = 32;
// Define the source property for the instance of BmpOptions
bmpCreateOptions.Source = new StreamSource();
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
Image image = Image.Create(bmpCreateOptions, 500, 500);
// Create and initialize an instance of Graphics class
Graphics graphic = new Graphics(image);
// Clear the image surface with White color
graphic.Clear(Color.White);
// Draw a dotted rectangle shape by specifying the Pen object having red
// color and a rectangle structure
graphic.DrawRectangle(new Pen(Color.Red, 3),
new Rectangle(60, 40, 70, 120));
// Draw a continuous rectangle shape by specifying the Pen object having
// solid brush with blue color and a rectangle structure
graphic.DrawRectangle(new Pen(new SolidBrush(Color.Blue), 3),
new Rectangle(40, 60, 120, 70));
// Save all changes
image.Save("draw_reactangle.bmp");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment