上传图像时颜色变化

时间:2013-10-13 01:13:56

标签: c# asp.net image-processing graphics system.drawing

我使用的是一种从互联网上获取的方法,我对它进行了一些定制。

它需要来自fileUpload的HttpPostedFile和一些不必要的参数,然后调整图像大小并对其进行处理,然后将其保存在托管中并返回位置

但是在我上传图片之后我发现它变得有点灰了,你可以在这里看到两张图片的区别。

真实图片 enter image description here

上传的图片 Uploaded Image

我如何在我的方法中修复它。

我的上传方法

public string ResizeImage(HttpPostedFile PostedFile, string destinationfile, int maxWidth, int maxHeight)
{

    float ratio;

    // Create variable to hold the image
    System.Drawing.Image thisImage = System.Drawing.Image.FromStream(PostedFile.InputStream);

    // Get height and width of current image
    int width = (int)thisImage.Width;
    int height = (int)thisImage.Height;

    // Ratio and conversion for new size
    if (width < maxWidth)
    {
        ratio = (float)width / (float)maxWidth;
        width = (int)(width / ratio);
        height = (int)(height / ratio);
    }

    // Ratio and conversion for new size
    if (height < maxHeight)
    {
        ratio = (float)height / (float)maxHeight;
        height = (int)(height / ratio);
        width = (int)(width / ratio);
    }

    // Create "blank" image for drawing new image
    System.Drawing.Bitmap outImage = new System.Drawing.Bitmap(width, height);
    System.Drawing.Graphics outGraphics = System.Drawing.Graphics.FromImage(outImage);
    System.Drawing.SolidBrush sb = new System.Drawing.SolidBrush(System.Drawing.Color.White);

    outGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    outGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    outGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

    // Fill "blank" with new sized image
    outGraphics.FillRectangle(sb, 0, 0, outImage.Width, outImage.Height);
    outGraphics.DrawImage(thisImage, 0, 0, outImage.Width, outImage.Height);
    sb.Dispose();
    outGraphics.Dispose();
    thisImage.Dispose();

    if (!destinationfile.EndsWith("/"))
        destinationfile += "/";

    if (!System.IO.Directory.Exists(Server.MapPath(destinationfile)))
        System.IO.Directory.CreateDirectory(Server.MapPath(destinationfile));

    // Save new image as jpg
    string filename = Guid.NewGuid().ToString();
    outImage.Save(Server.MapPath(destinationfile + filename + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
    outImage.Dispose();

    return destinationfile + filename + ".jpg";
}

修改

我拿了一个打印屏幕,这样你就可以看到两张图片之间的颜色差异

enter image description here

1 个答案:

答案 0 :(得分:1)

图像在颜色方面的主要区别在于原始图像根本没有颜色配置文件,而处理后的图像具有sRGB颜色配置文件。

根据浏览器,操作系统以及屏幕校准方式的不同,图像的颜色会略有不同。对于没有颜色配置文件的图像,浏览器可以为其假定颜色配置文件,也可以显示它而不进行任何颜色校正。当我在计算机上查看具有颜色校准屏幕的Firefox中的图像时,实际上根本看不到任何颜色差异。

当您保存图像时,JPEG编码器已采用sRGB颜色配置文件,这与原始图像中根本没有颜色配置文件信息时的任何其他配置文件一样好。您需要上传带有颜色配置文件的图像,以查看JPEG编码器是否正确处理。只要没有颜色配置文件,在解释图像中的颜色值时就没有对错。