Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# rotate bitmap 90 degrees

Tags:

c#

bitmap

I'm trying to rotate a bitmap 90 degrees using the following function. The problem with it is that it cuts off part of the image when the height and width are not equal.

Notice the returnBitmap width = original.height and it's height = original.width

Can anyone help me solve this issue or point out what I'm doing wrong?

    private Bitmap rotateImage90(Bitmap b)     {         Bitmap returnBitmap = new Bitmap(b.Height, b.Width);         Graphics g = Graphics.FromImage(returnBitmap);         g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);         g.RotateTransform(90);         g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);         g.DrawImage(b, new Point(0, 0));         return returnBitmap;     } 
like image 859
Kevin Avatar asked Feb 08 '10 22:02

Kevin


1 Answers

What about this:

private void RotateAndSaveImage(String input, String output) {     //create an object that we can use to examine an image file     using (Image img = Image.FromFile(input))     {         //rotate the picture by 90 degrees and re-save the picture as a Jpeg         img.RotateFlip(RotateFlipType.Rotate90FlipNone);         img.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg);     } } 
like image 140
Rubens Farias Avatar answered Oct 06 '22 01:10

Rubens Farias