0

I am trying to rotate a image after scanning to improve the readability of scanned images if the skew angle is too high. After finding out the Deskew angle i am trying to rotate the image with the following Code.

After rotating the image and saving it subsequently , my image size is increase closed to 10 times.

What is the reason for the increase in the File Size of an image ?

   public  Bitmap RotateImage(Image inputImage, float angleDegrees, bool upsizeOk,
                                 bool clipOk, Color backgroundColor)
    {
    /// Method to rotate an Image object. The result can be one of three cases:
    /// - upsizeOk = true: output image will be larger than the input, and no clipping occurs 
    /// - upsizeOk = false & clipOk = true: output same size as input, clipping occurs
    /// - upsizeOk = false & clipOk = false: output same size as input, image reduced, no clipping
    /// 

    // Test for zero rotation and return a clone of the input image
    if (angleDegrees == 0f)
    return (Bitmap)inputImage.Clone();

    // Set up old and new image dimensions, assuming upsizing not wanted and                         clipping OK
    int oldWidth = inputImage.Width;
    int oldHeight = inputImage.Height;
    int newWidth = oldWidth;
    int newHeight = oldHeight;
    float scaleFactor = 1f;

    // If upsizing wanted or clipping not OK calculate the size of the resulting bitmap
    if (upsizeOk || !clipOk)
    {
    double angleRadians = angleDegrees * Math.PI / 180d;

    double cos = Math.Abs(Math.Cos(angleRadians));
    double sin = Math.Abs(Math.Sin(angleRadians));
    newWidth = (int)Math.Round(oldWidth * cos + oldHeight * sin);
    newHeight = (int)Math.Round(oldWidth * sin + oldHeight * cos);
    }

    // If upsizing not wanted and clipping not OK need a scaling factor
    if (!upsizeOk && !clipOk)
    {
     scaleFactor = Math.Min((float)oldWidth / newWidth, (float)oldHeight / newHeight);
     newWidth = oldWidth;
     newHeight = oldHeight;
        }

        // Create the new bitmap object. If background color is transparent it must be 32-bit, 
        //  otherwise 24-bit is good enough.
        Bitmap newBitmap = new Bitmap(newWidth, newHeight, backgroundColor == Color.Transparent ?
                                         PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb);
        newBitmap.SetResolution(inputImage.HorizontalResolution, inputImage.VerticalResolution);



        // Create the Graphics object that does the work
        using (Graphics graphicsObject = Graphics.FromImage(newBitmap))
        {
           // graphicsObject.InterpolationMode = InterpolationMode.HighQualityBicubic;
           // graphicsObject.PixelOffsetMode = PixelOffsetMode.HighQuality;
           // graphicsObject.SmoothingMode = SmoothingMode.HighQuality;

            // Fill in the specified background color if necessary
            if (backgroundColor != Color.Transparent)
                graphicsObject.Clear(backgroundColor);

            // Set up the built-in transformation matrix to do the rotation and maybe scaling
            graphicsObject.TranslateTransform(newWidth / 2f, newHeight / 2f);

            if (scaleFactor != 1f)
                graphicsObject.ScaleTransform(scaleFactor, scaleFactor);

            graphicsObject.RotateTransform(angleDegrees);
            graphicsObject.TranslateTransform(-oldWidth / 2f, -oldHeight / 2f);

            // Draw the result 
            graphicsObject.DrawImage(inputImage, 0, 0);
        }

        return newBitmap;
    }

And i am Saving the Bitmap with the following method

   bmp.Save("C:\\Users\\pamit\\Desktop\\FileName.Tiff", jpgEncoder, myEncoderParameters);
amithkrp07
  • 81
  • 1
  • 6
  • Did you have a question? – Ben Voigt Apr 10 '15 at 14:52
  • Yep :).. I wanted to understand why there is increase in the file size ? – amithkrp07 Apr 10 '15 at 14:56
  • Because you decompressed the original input, and then when you saved it to disk, you didn't compress it by as much. – Ben Voigt Apr 10 '15 at 14:57
  • related: http://stackoverflow.com/q/22387681/103167 – Ben Voigt Apr 10 '15 at 15:00
  • Ben , Could you please tell me where am i decompressing the image and I already tried Encoder.Compression parameter and i tried with 100L – amithkrp07 Apr 10 '15 at 15:01
  • Compression = 100 is full quality (no compression). Decompression happened when you read the original file into a `Bitmap` object. – Ben Voigt Apr 10 '15 at 15:04
  • Hi Ben , thanks a lot for your input. I tried with CompressionCCITT4 and CompressionCCITT3 for Compressing Tiff images and found there was not much difference with File Size. I will look into the links mentioned above – amithkrp07 Apr 10 '15 at 15:09
  • Why don't you use png??? No loss always very nice compression.. Of course nobody can give you an exact explanation without knowing or seeing the original file parameters.. If all goes really well and you set the scanned letters straight you should expect a reduction, at least if you have a proper gamma to make the paper white.. – TaW Apr 10 '15 at 17:02
  • The png compression in the .Net framework is very unoptimised though. Always gives bigger files than the original, even without manipulation. – Nyerguds Sep 12 '17 at 08:09

1 Answers1

0

My guess is that you have a very high quality in your encoding parameters.

Try saving it with something like:

EncoderParameters myEncoderParameters = new EncoderParameters(1);

EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;

This will save it with a 50% jpeg quality. You can try to tune it to get the desired quality vs size.

Gerard Abello
  • 658
  • 3
  • 7
  • Hi Gerard , I am already doing it.. System.Drawing.Imaging.Encoder myEncoder; myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L); myEncoderParameters.Param[0] = myEncoderParameter; ImageCodecInfo myImageCodecInfo = GetEncoder(ImageFormat.Tiff); temp.Save("C:\\Desktop\\New folder\\1.Tiff", myImageCodecInfo, myEncoderParameters); And no luck with size reduction :( – amithkrp07 Apr 10 '15 at 15:43
  • I'm not used to the Tiff file format, but it can hold both lossy (low file size) and lossless (high file size) picture formats. Maybe you are reading a lossy file (a jpeg for example) and saving it as a lossless tiff file. – Gerard Abello Apr 13 '15 at 07:15