In the last four articles of this series we discussed how to load an image in our Windows form, resize an image, crop an image and how to set the brightness of an image; see:
Part1: Open an image
Part2: Resizing image
Part3: Cropping image
Part 4: brightness of image
This article is the next part of how to make an image editor C#.
In this article we will discuss how to rotate, flip and save an image.
Form Designer:
Put four buttons on a form like this:
Code:
We are using here the RotateFlip() method for rotating images; the RotateFlip method accepts a RotateFlipType enumeration that has these members (Ref: MDSN):
Rotate90FlipNone |
Specifies a 90-degree clockwise rotation without flipping. |
Rotate180FlipNone |
Specifies a 180-degree clockwise rotation without flipping. |
Rotate270FlipNone |
Specifies a 270-degree clockwise rotation without flipping. |
RotateNoneFlipX |
Specifies no clockwise rotation followed by a horizontal flip. |
Rotate90FlipX |
Specifies a 90-degree clockwise rotation followed by a horizontal flip. |
Rotate180FlipX |
Specifies a 180-degree clockwise rotation followed by a horizontal flip. |
Rotate270FlipX |
Specifies a 270-degree clockwise rotation followed by a horizontal flip. |
RotateNoneFlipY |
Specifies no clockwise rotation followed by a vertical flip. |
Rotate90FlipY |
Specifies a 90-degree clockwise rotation followed by a vertical flip. |
Rotate180FlipY |
Specifies a 180-degree clockwise rotation followed by a vertical flip. |
Rotate270FlipY |
Specifies a 270-degree clockwise rotation followed by a vertical flip. |
RotateNoneFlipXY |
Specifies no clockwise rotation followed by a horizontal and vertical flip. |
Rotate90FlipXY |
Specifies a 90-degree clockwise rotation followed by a horizontal and vertical flip. |
Rotate180FlipXY |
Specifies a 180-degree clockwise rotation followed by a horizontal and vertical flip. |
Rotate270FlipXY |
Specifies a 270-degree clockwise rotation followed by a horizontal and vertical flip. |
Generate a click event for all four buttons and try to apply these code snippets:
private void btnRotateLeft_Click(object sender, EventArgs e)
{
PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
PictureBox1.Refresh();
}
private void btnRotateRight_Click(object sender, EventArgs e)
{
PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
PictureBox1.Refresh();
}
private void btnRotateHorizantal_Click(object sender, EventArgs e)
{
PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
PictureBox1.Refresh();
}
private void btnRotatevertical_Click(object sender, EventArgs e)
{
PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipY);
PictureBox1.Refresh();
}
You can download source code for description.
One more thing; the RotateFlip method rotates the image clockwise.