0

如果倾斜角度太高,我正在尝试在扫描后旋转图像以提高扫描图像的可读性。找出偏斜角度后,我尝试使用以下代码旋转图像。

旋转图像并随后保存后,我的图像大小增加了接近 10 倍。

图像文件大小增加的原因是什么?

   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;
    }

我正在使用以下方法保存位图

   bmp.Save("C:\\Users\\pamit\\Desktop\\FileName.Tiff", jpgEncoder, myEncoderParameters);
4

1 回答 1

0

我的猜测是您的编码参数质量非常高。

尝试使用以下方式保存它:

EncoderParameters myEncoderParameters = new EncoderParameters(1);

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

这将以 50% 的 jpeg 质量保存它。您可以尝试调整它以获得所需的质量与尺寸。

于 2015-04-10T15:21:57.393 回答