ARTICLE

Rotate and Flip Images in Windows Forms

Posted by Shubham Saxena Articles | Windows Forms C# October 04, 2012
This is the step-by-step process of how to use builtin functions for rotating and flipping an image.
Reader Level:

One of my friends asked me how to rotate an image to a particular angle, so I just thought of writing an article about it. The following is the step-by-step process of how to use builtin functions for rotating and flipping an image.

Step 1

Create a blank Windows Forms application, as in:

Windows-forms-application.jpg

Step 2

Add a pictureBox from the toolbox and add a source image to it.

Now comes the real part, using Image.RotateFlip we can rotate the image by standard angles (90, 180 or 270 degrees) or flip the image horizontally or vertically.

The parameter of RotateFlip is System.Drawing.RotateFlipType, which specifies the type of rotation and flip to apply to the image.

e.g. : pictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);

The preceding code will rotate the image in a pictureBox1 by an angle of 180 degrees.

Step 3

Now back to our code, add the namespaces to use as:

using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

Step 4

We will make a function RotateImg with two arguments one is the Bitmap Image and the other is the float angle. The code snippet is as follows :

public static Bitmap RotateImg(Bitmap bmp, float angle)

{

int w = bmp.Width;

int h = bmp.Height;

Bitmap tempImg = new Bitmap(w, h);

Graphics g = Graphics.FromImage(tempImg);

g.DrawImageUnscaled(bmp, 1, 1);

g.Dispose();

GraphicsPath path = new GraphicsPath();

path.AddRectangle(new RectangleF(0f, 0f, w, h));

Matrix mtrx = new Matrix();

mtrx.Rotate(angle);

RectangleF rct = path.GetBounds(mtrx);

Bitmap newImg = new Bitmap(Convert.ToInt32(rct.Width), Convert.ToInt32(rct.Height));

g = Graphics.FromImage(newImg);

g.TranslateTransform(-rct.X, -rct.Y);

g.RotateTransform(angle);

g.InterpolationMode = InterpolationMode.HighQualityBilinear;

g.DrawImageUnscaled(tempImg, 0, 0);

g.Dispose();

tempImg.Dispose();

return newImg;

}

Step 5

Now we have a function which will rotate an image to an arbitrary angle, I'm adding my code now to the form_load event so that as soon as the form loads my code is executed.

Bitmap bitmap = (Bitmap)pictureBox1.Image;
pictureBox1.Image = (Image)(RotateImg(bitmap, 30.0f));

The preceding two lines of code will first convert the image in pictureBox1 to a Bitmap image and then it will call our function RotateImg to rotate the image 30 degrees (for example).

Here is the snapshot of the output window:

output-Windows-forms-application.jpg

Login to add your contents and source code to this article
post comment
     

use the same events and change the code inside them.... i.e use imagebox1.ImageLocation = "your location";

Posted by Shubham Saxena Jan 26, 2013

Thank u so much....Its really helpful. And I am in need of another help in changing the images also ie already an image will be set to a button and by placing the mouse over the button the image should also be altered. How to import both the images

Posted by Keerthana Bhaskaran Jan 22, 2013

create two events, MouseHover() and MouseLeave(), use button1.BackColor = Color.Aqua; to change color according to your choice.. Done. :)

Posted by Shubham Saxena Jan 19, 2013

Hi, This is urgent. pls help me. I have got a scenario. There will be a button filled with a colour placed in the windows form, the colour of the button should change when we move the mouse and place that over the button.(the changes should happen only when moving the mouse and placing over the button and not by clicking the button)I am told that it can be done in two ways either by writing a code or by making some changes in the properties. So could u pls help in both the ways since I am new to this. pls give me complete codings so that i can learn and try for different scenarios Thanks in advance

Posted by Keerthana Bhaskaran Jan 19, 2013

thanx :)

Posted by Shubham Saxena Oct 04, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter