locked
How do I rotate or flip images? RRS feed

Answers

  • Using the Image.RotateFlip method, we can rotate the image by 90/180/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.

    Code Snippet:

    //Specifies a 180-degree rotation without flipping
    pictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
    //Specifies no rotation followed by a horizontal flip
    pictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
    //Specifies a 90-degree rotation followed by a horizontal and vertical flip pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipXY);
    
    

     

    If you want to rotate an image by any degree, we can use System.Drawing.Drawing2D.Matrix

    Code Snippet:

     public static Bitmap RotateImg(Bitmap bmp, float angle, Color bkColor)
            {
                int w = bmp.Width;
                int h = bmp.Height;
                PixelFormat pf = default(PixelFormat);
                if (bkColor == Color.Transparent)
                {
                    pf = PixelFormat.Format32bppArgb;
                }
                else
                {
                    pf = bmp.PixelFormat;
                }
    
                Bitmap tempImg = new Bitmap(w, h, pf);
                Graphics g = Graphics.FromImage(tempImg);
                g.Clear(bkColor);
                g.DrawImageUnscaled(bmp, 1, 1);
                g.Dispose();
    
                GraphicsPath path = new GraphicsPath();
                path.AddRectangle(new RectangleF(0f, 0f, w, h));
                Matrix mtrx = new Matrix();
                //Using System.Drawing.Drawing2D.Matrix class 
                mtrx.Rotate(angle);
                RectangleF rct = path.GetBounds(mtrx);
                Bitmap newImg = new Bitmap(Convert.ToInt32(rct.Width), Convert.ToInt32(rct.Height), pf);
                g = Graphics.FromImage(newImg);
                g.Clear(bkColor);
                g.TranslateTransform(-rct.X, -rct.Y);
                g.RotateTransform(angle);
                g.InterpolationMode = InterpolationMode.HighQualityBilinear;
                g.DrawImageUnscaled(tempImg, 0, 0);
                g.Dispose();
                tempImg.Dispose();
                return newImg;
            }
            public Form1()
            {
                InitializeComponent();
                pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
                pictureBox1.Image = Image.FromFile(@"C:\image.jpg");
                pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
                Bitmap bitmap = (Bitmap)pictureBox1.Image;
                pictureBox1.Image = (Image)(RotateImg(bitmap, 30.0f, Color.Transparent));
            }
    
    

     

    For more FAQ about Visual C# General, please see Visual C# General FAQ

     



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 8, 2009 6:44 AM