为什么这个C#图像处理程序会改变图像的颜色?

时间:2015-07-13 15:50:18

标签: c# asp.net image-processing

我有以下图像处理程序方法来调整大小并保存图像,但是它会更改图像的颜色以供注意。我作为质量值传递100。我也可能会失去理智,但我发誓有时它不会影响颜色。然而,我运行的最后10次测试确实如此。

public string Save(Bitmap image, int maxWidth, int maxHeight, int quality, string pathWithName, out int iWidth, out int iHeight)
    {

        // Get the image's original width and height
        int originalWidth = image.Width;
        int originalHeight = image.Height;
        int newWidth = 0;
        int newHeight = 0;

        if (originalWidth < maxWidth && originalHeight < maxHeight)
        {
            newWidth = originalWidth;
            newHeight = originalHeight;

        }
        else
        {
            // To preserve the aspect ratio
            float ratioX = (float)maxWidth / (float)originalWidth;
            float ratioY = (float)maxHeight / (float)originalHeight;
            float ratio = Math.Min(ratioX, ratioY);

            // New width and height based on aspect ratio
            newWidth = (int)(originalWidth * ratio);
            newHeight = (int)(originalHeight * ratio);
        }
        // Convert other formats (including CMYK) to RGB.
        Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);

        // Draws the image in the specified size with quality mode set to HighQuality
        using (Graphics graphics = Graphics.FromImage(newImage))
        {
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.DrawImage(image, 0, 0, newWidth, newHeight);
        }

        // Get an ImageCodecInfo object that represents the JPEG codec.
        ImageCodecInfo imageCodecInfo = this.GetEncoderInfo(ImageFormat.Jpeg);

        // Create an Encoder object for the Quality parameter.
        Encoder encoder = Encoder.Quality;

        // Create an EncoderParameters object. 
        EncoderParameters encoderParameters = new EncoderParameters(1);

        // Save the image as a JPEG file with quality level.
        EncoderParameter encoderParameter = new EncoderParameter(encoder, quality);
        encoderParameters.Param[0] = encoderParameter;
        newImage.Save(pathWithName, imageCodecInfo, encoderParameters);

        iWidth = newWidth;
        iHeight = newHeight;
        return pathWithName;
    }

0 个答案:

没有答案