Как нарисовать круг в c windows forms
Перейти к содержимому

Как нарисовать круг в c windows forms

  • автор:

Draw Circles in C#

Draw Circles in C#

In this article, we will be looking at how we can draw a circle in C#.

Use the Drawing.Ellipse() Method to Draw Circles in C#

System.Drawing doesn’t have an explicit circle draw. We can use the Drawing.Ellipse() method, which provides the same functionality or create a new FORM with Windows.FORMS(.NET FRAMEWORK) to allow us to experiment with interfaces.

Draw Circles in C# Using .NET Framework

Ensure that the Paint method is called when you have booted into a FORM . Double click on the form, and the properties will open up.

Switch over to the EVENTS section.

Inside the EVENTS section now, scroll down till you find the PAINT and double click on it to produce the PAINT function.

Now, we create our ELLIPSE using the System.Drawing and then choose the Ellipse option.

We have chosen a new PEN in the parameters with the color red. The next parameter tends to draw the RECTANGLE encapsulating the circle.

Think of it like the size of the circle, with the first two parameters denoting the origin points ( x and y ) and the last two parameters being the size of the x-axis and the y-axis.

Use the FillEllipse() Method to Fill Circles in C#

To fill the circle on the above output, we will use the FILLELLIPSE() function.

We defined the same points for the FillEllipse() function as we did for the Drawing.Ellipse() to ensure that the correct area is filled. We have chosen the Brushes.Red brush for it as the first parameter.

Full Code Snippet:

That is how you draw a circle in C#. We hope you learned this well and can modify it as per your needs.

Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!

C# Drawing circles in a panel

i am doing a program on the math «Problem of Apollonius». But first my program needs to be able to allow the users to draw three circles on a panel, the circles can differ from size and position.

I cant figure out how to allow the users to draw their on size circle on the panel. Any help would be greatly appreciated.

5 Answers 5

Here is a simple demonstration for Windows Forms.

  1. In the MouseDown event, capture the location of the center (or top corner of bounding box, according to preference) of the circle, and the fact that a circle is being drawn.
  2. In the MouseMove event, if a circle is being drawn, then draw the circle, using the current mouse location as a point on the edge of the circle (or opposite corner of bounding box, according to preference)
  3. In the MouseUp event, capture the radius of the circle and the fact that it is no longer being drawn. Store the newly created circle in the circle collection, and render it to the screen.

In some older technologies, you would have to erase and redraw the circle in the MouseMove event of step 2. If you are using WPF, or something similarly advanced, you can just create a circle object and add it to a canvas in step 1, and then manipulate its properties in step 2. The WPF engine will take care of erasing the circle from the old location and drawing it in the new location.

Как нарисовать круг в c windows forms

Gray Pipe

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Asked by:


I have a WinForms application that interacts with a connection. If the connection is fine I want to show a green filled circle, if not I want to show a red filled circle.

I found no circle element in the toolbox so I think I have to draw it on my own.

I created a picture box called picBoxClientState and started with this code

The debugger reaches the line of code but nothing gets drawn. How can I draw on this element whenever I call CheckSignedInState() ?

Maybe there is a better way instead of drawing on a picturebox? (I don’t want to toggle two images because there might be more states to draw)

  • Edited by MH_Computech Tuesday, August 14, 2018 9:24 AM

All replies

For example to draw a green circle, you can do :

  • Proposed as answer by phil chelis Thursday, August 16, 2018 7:31 AM

I suggest not using GDI at all. In my experience it doesnt work well and sometimes my projects just

fail because the draws fail. However, you could use a variety of pre-drawn bitmaps/icon of various

sizes for screen DPI.

The basic project setup is import your custom bitmap/icons as a Resource. Place them in a container

class like Dictionary<ConnectionState,bitmap>. Where ConnectionState is an enumeration , that also

has the flex for additional elements. Same is said about the Resource and class Dictionary<>.

The above project code makes use of premade Bitmaps import from project resources. With varying

colors and 2 designs to show an LED on/off state. However, this version uses 8 x 8 bitmaps that are

set as PictureBox.Image properties. See below;

The above syntax example assumes the container is a PictureBox but you can

use with Button class or others supporting Image properties.

I use similiar methodology because GDI sometimes fails and so does the project. Many will

have different opinions. So you will have to decide what works best for your projects goal.

How to Add Graphics to a C# Windows Form Application

See how you can draw shapes, use colors, and render images in a WinForms app.

Windows Forms is a framework that lets you build desktop applications. You can click and drag components like buttons onto a visual user interface. It also helps you manually create various shapes within your code.

This article will show you how to add lines, shapes, and images to your application. This tutorial uses the Visual Studio 2019 Community Edition to show examples.

What Are the Built-In Classes Used for Drawing Graphics?

Windows Forms uses the C# programming language. Its built-in classes and methods allow you to draw various shapes onto a Windows Form canvas. These include the Graphics, Pen, Color, and Brush classes.

The Graphics class allows you to draw shapes and lines onto the canvas. It includes methods such as:

  • DrawLine(Pen, Point1, Point2)
  • DrawRectangle(x, y, width, height)
  • DrawPolygon(Pen, PointF[])

The Pen class allows you to specify the properties of a 'pen' tip which you can use to draw your shapes. You can specify properties such as color, thickness, or dash style. Methods include:

    SetLineCap(LineCap, LineCap, DashCap)

A color object made up of R (red), G (green), and B (blue) values. You will need a color object for many of the built-in methods that create shapes.

SolidBrush, HatchBrush, TextureBrush

These brush classes derive from the "Brush" interface. These classes allow you to color in blank spaces on the canvas. You can also choose to fill the spaces using different patterns or textures. You can specify properties such as the color.

Rectangle, Line, Polygon, Ellipse

You can create objects based on these shapes, and use them when calling methods such as DrawRectangle(). Instead of passing the x, y, width, and height as arguments, you can choose to pass an existing Rectangle object instead.

To view the source code for a running example of the above tutorial, visit the GitHub repository. You can try out the following examples once you've created a Winforms application.

How to Add a Paint on Form Load Event Handler

First, add an event handler to draw shapes when the canvas loads.

  1. Add a Paint function for the form.
  2. Go into the Design View Tab.
  3. In the Properties window, select the lightning icon to open the "Events" tab.
  4. In "Paint", under "Appearance", select the Form1_Paint function. This will execute the function when you run the application.

How to Draw Lines Onto a Windows Form Canvas

You can use a Color, Pen, and the DrawLine() method to draw lines on a canvas.

  1. Inside the Form1_Paint() function, create a Color object with the color you want the line to be. Then, create a Pen object to draw the line with.
  2. The DrawLine() method from the Graphics class will draw a line using the pen. This will start drawing a line from an x, y position to another x, y position.
  3. You can modify the properties for the pen object to change its width, dash style, and start or end cap.
  4. Press the green play button at the top of Visual Studio to see the changes.

How to Draw Shapes Such as Rectangles and Circles

You can use the shapes classes for different shapes, or draw shapes manually onto the canvas.

  1. Create a Color and Pen object as shown in the previous steps. Then, use the DrawRectangle() method to create the rectangle. The arguments are the x and y coordinates for the top-left of the rectangle, along with its width and height.
  2. You can also create a rectangle using the Rectangle Class. First, create a Rectangle object. The arguments are also the x and y coordinates for the top-left corner, width, and height.
  3. Use the DrawRectangle() function to draw the rectangle. Instead of passing the x, y, width, and height like before, you can use the Rectangle object instead.
  4. Press the green play button at the top of Visual Studio to see the changes.

How to Use the Brush Class to Fill In Shapes With Color

You can use the FillRectangle(), FillEllipses() or FillTriangle() methods to create shapes with a solid color.

  1. First, create a brush object.
  2. Use the FillRectangle(), FillEllipses() or FillTriangle() methods. They work the same way as the draw functions above, except instead of a Pen, they use a Brush object.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *