Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image resize when rotate

I'm trying to rotate the image.. I have a pictureBox 369x276. But when I rotate, this size decrease.

The pictureBox sizeMode is PictureBoxSizeMode.StretchImage

here is my code:

        Bitmap oldBitmap = (Bitmap)pictureBox1.Image;
        float angle = 90;
        var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);

        var graphics = Graphics.FromImage(newBitmap);
        graphics.TranslateTransform((float)oldBitmap.Width  / 2, (float)oldBitmap.Height / 2);
        graphics.RotateTransform(angle);
        graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
        graphics.DrawImage(oldBitmap, new Point(0, 0));
        pictureBox1.Image = newBitmap;
like image 672
Ladessa Avatar asked Mar 14 '13 17:03

Ladessa


People also ask

How do I rotate a picture without losing quality?

To perform a lossless left/right rotate or vertical/horizontal flip, go to Tools > JPEG Lossless Rotate. Alternatively, you can also find the Tools menu by right clicking on the image. The rotate clockwise or counterclockwise icons are also lossless for JPEG files.

Does rotating a photo reduce quality?

All digital edits even tiny ones, cause some loss of data. Usually this data loss is in visible and therefore results in no loss of quality. You can rotate considerably, and more than a couple times with no visible loss in quality. For raster images: In theory, yes.

How do I resize and rotate a picture in a shape?

Manually rotate a picture or shape Select the picture or shape. Manually rotate the text box by selecting the shape or picture rotation handle and dragging in the direction you want. To keep the rotation to 15 degree angles, press and hold Shift while you drag the rotation handle.

How do I rotate an image in Photoshop without distorting it?

There are a few different ways that you can rotate an image in Photoshop without rotating the canvas. One way is to go to Image > Image Rotation > 180. This will rotate the image clockwise by 180 degrees. Another way is to go to Edit > Transform > Rotate 90 CW.


2 Answers

Just use RotateFlip:

Bitmap oldBitmap = (Bitmap)pictureBox1.Image;
oldBitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
pictureBox1.Image = oldBitmap;

As @Dan-o has pointed out, this allows a rotation of any of the degree's in the System.Drawing.RotateFlipType enum.

To rotate a Bitmap any angle without losing the size, you could do the following, but it's a bit convoluted!

One - Add the WriteableBitmapEx library to your project

Two - Add the XAML, WindowsBase and PresentationCore libraries to your project

Three - Use the following to rotate your Bitmap any amount of degrees:

class Program
{
    static void Main(string[] args)
    {
        Bitmap oldBitmap = (Bitmap)pictureBox1.Image;;

        var bitmapAsWriteableBitmap = new WriteableBitmap(BitmapToBitmapImage(oldBitmap));
        bitmapAsWriteableBitmap.RotateFree(23);

        var rotatedImageAsMemoryStream = WriteableBitmapToMemoryStream(bitmapAsWriteableBitmap);
        oldBitmap = new Bitmap(rotatedImageAsMemoryStream);
    }

    public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
    {
        var memStream = BitmapToMemoryStream(bitmap);
        return MemoryStreamToBitmapImage(memStream);
    }

    public static MemoryStream BitmapToMemoryStream(Bitmap image)
    {
        var memoryStream = new MemoryStream();
        image.Save(memoryStream, ImageFormat.Bmp);

        return memoryStream;
    }

    public static BitmapImage MemoryStreamToBitmapImage(MemoryStream ms)
    {
        ms.Position = 0;
        var bitmap = new BitmapImage();

        bitmap.BeginInit();

        bitmap.StreamSource = ms;
        bitmap.CacheOption = BitmapCacheOption.OnLoad;

        bitmap.EndInit();
        bitmap.Freeze();

        return bitmap;
    }

    private static MemoryStream WriteableBitmapToMemoryStream(WriteableBitmap writeableBitmap)
    {
        var ms = new MemoryStream();

        var encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(writeableBitmap));

        encoder.Save(ms);

        return ms;
    }
}

Pain in the ass, but works!

like image 190
JMK Avatar answered Oct 07 '22 18:10

JMK


The smaller image size is to be expected. I've never figured out why, but the Graphics.DrawImage really only works if you provide it not only a start location, but also a size. One of the overloads allows you to include size.

like image 30
Sam Axe Avatar answered Oct 07 '22 18:10

Sam Axe