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!


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 2014


    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.