Create sized oval images in C#

[oval images]

Almost as soon as I had finished the example Create oval images in C#, I realized that it wouldn’t work for my purposes. I wanted to make oval images for several people. Unfortunately, that program didn’t let you make images of the same size, so I created this program.

Instead of having you select an oval by clicking and dragging, this version lets you enter the oval’s width and height. You can then drag the oval into position to select part of the picture.

The following code shows the parts of the program that deal with moving the oval.

private Rectangle OvalRect =
    new Rectangle(0, 0, 200, 250);

// Select an elliptical area.
private bool Dragging = false;
private Point MousePos;
private void picImage_MouseDown(object sender, MouseEventArgs e)
{
    Dragging = true;
    MousePos = e.Location;
}

private void picImage_MouseMove(object sender, MouseEventArgs e)
{
    if (!Dragging) return;
    OvalRect.X += e.Location.X - MousePos.X;
    OvalRect.Y += e.Location.Y - MousePos.Y;
    MousePos = e.Location;

    DrawImage();
}

private void picImage_MouseUp(object sender, MouseEventArgs e)
{
    Dragging = false;
}

The variable OvalRect holds the oval’s location.

The MouseDown event handler sets Dragging to true so the program remembers that a drag is in progress. It then saves the mouse’s current position in variable MousePos.

Teh MouseMove event handler first checks Dragging to see if a drag is in progess and returns if one is not. It then calculates the distance that the mouse has moved since the last time its position was recorded and moves OvalRect by that amount. It finishes by calling DrawImage to draw the original image with the oval on it.

The MouseUp event handler simply sets Dragging to false to indicate that the drag is no longer in progress.

The following code shows how the DrawImage method draws the image with the oval.

private void DrawImage()
{
    Bitmap bm = new Bitmap(OriginalImage);
    using (Graphics gr = Graphics.FromImage(bm))
    {
        gr.SmoothingMode = SmoothingMode.AntiAlias;
        gr.DrawEllipse(Pens.Magenta, OvalRect);
    }
    picImage.Image = bm;
}

[oval images]

This method creates a new Bitmap that is a copy of the original image as loaded from a file. It then creates an associated Graphics object and sets its SmoothingMode property so it can draw a smooth oval. The code then calls the Graphics object’s DrawEllipse method to draw the ellipse at its current position. It finishes by displaying the image in the picImage PictureBox.

The rest of the example is the same as the previous post. Visit that example and download this one to see additional details.


Download Example   Follow me on Twitter   RSS feed   Donate



About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in drawing, image processing and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.