draw a rectangle - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

draw a rectangle


Hello to all

I would like to create an application that asks the user to type the length and the width of a rectangle in a field and then he can click on a first button(draw rectangle) to draw the rectangle in question and click again on the second button to draw a horizontal line that divides the rectangle in two equal parts.

Thank you to help me please.


  • TeejayTeejay South AfricaPosts: 2Member
    edited June 19


    using System.Drawing
    Size(Width, Height)
    Point(X, Y)
    Rectangle(Point, Size)
    Line(Point1, Point2)

    [Place this in the class]

    //the width of the rectangle
    private int width = 100;
    //the height of the rectangle
    private int height = 100;
    //create an empty bitmap object
    private Bitmap b = new Bitmap(width, height);
    //create graphics object from bitmap
    private Graphics g = Graphics.FromImage(b);

    [Button 1]

    //draw a rectangle on the bitmap
    g.DrawRectangle(Pens.Black, new Rectangle(new Point(0, 0), new Size(b.Width, b.Height)));
    //assign bitmap to the image property of any object
    pictureBox1.BackgroundImage = b;

    [Button 2]

    //draw line horizontally in the middle
    g.DrawLine(Pens.Black, new Point(0, height / 2), new Point(width, height / 2))
Sign In or Register to comment.