1

I have the following code to rotate an image in C#:

    using (var stream = new FileStream(path, FileMode.Open))
    {
        using (var image = Image.FromStream(stream))
        {
            stream.Close();
            image.RotateFlip(rotateType);
            image.Save(path1, ImageFormat.Png);
            image.Dispose();
        }
    }

If the original file size was 700 KiB, the new rotated file has size of 7+ MiB.

What is wrong with this code? Any help is much appreciated.

Update:

I tried changing the line image.Save(path1, ImageFormat.Png) to image.Save(path1) and image.Save(path1, image.RawFormt) with no improvement.

Babu James
  • 2,740
  • 4
  • 33
  • 50

2 Answers2

0

PNG is a bitmap file format:

  • higher filesize compared to jpg

Because of this you should safe your image as jpg:

Thus lossless PNG format is best suited for pictures still under edition - and the lossy formats, like JPEG, are best for the final distribution of photographic images, because in this case JPG files are usually smaller [...]

Source: wikipedia

Try safing the image in JPEG via:

image.Save(path, YourClass.GetImageFormat(image));

Tests:
Rotating an JPG file with this method and the size stays the same.
Rotating a 15.7MiB BMP file, the new size is ~800kiB.

To use the existing file format, use this extension method:

public static System.Drawing.Imaging.ImageFormat GetImageFormat(System.Drawing.Image img)
{             
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
        return System.Drawing.Imaging.ImageFormat.Jpeg;
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
        return System.Drawing.Imaging.ImageFormat.Bmp;
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
        return System.Drawing.Imaging.ImageFormat.Png;
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Emf))
        return System.Drawing.Imaging.ImageFormat.Emf;
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Exif))
        return System.Drawing.Imaging.ImageFormat.Exif;
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
        return System.Drawing.Imaging.ImageFormat.Gif;
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))
        return System.Drawing.Imaging.ImageFormat.Icon;
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.MemoryBmp))
        return System.Drawing.Imaging.ImageFormat.MemoryBmp;
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
        return System.Drawing.Imaging.ImageFormat.Tiff;
    else
        return System.Drawing.Imaging.ImageFormat.Wmf;            
}

Source: StackOverflow

Remember you have to look for the format before you manipulate the image.
Otherwise the image will be recognised as MemoryBmp.

using (var stream = new FileStream(path, FileMode.Open))
        {
            using (var image = Image.FromStream(stream))
            {
                stream.Close();
                var format = YourClass.GetImageFormat(image);
                image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                image.Save(path, format);
                image.Dispose();
            }
        }
Community
  • 1
  • 1
jAC
  • 5,195
  • 6
  • 40
  • 55
0

C# - How to change PNG quality or color depth

This guy's question looks similar to the same thing you are seeing.

Community
  • 1
  • 1