0

我正在使用从互联网上获得的方法,并对其进行了一些定制。

它从 fileUpload 获取一个 HttpPostedFile 和一些不必要的参数,然后调整图像大小并对其进行编译,然后将其保存在主机中并返回位置

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

真实图像: 在此处输入图像描述

上传图片 上传图片

我如何在我的方法中解决这个问题。

我的上传方法

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

编辑

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

在此处输入图像描述

4

1 回答 1

1

The main difference between the images when it comes to color, is that the original image doesn't have a color profile at all, while the processed image has the sRGB color profile.

Depending on the browser, the operating system, and how your screen is calibrated, the images will be shown with slightly different colors. For the image without a color profile, the browser can either assume a color profile for it, or it can display it without any color correction at all. When I view the images in Firefox on my computer that has a color calibrated screen, I actually can't see any color difference at all.

The JPEG encoder has assumed the sRGB color profile when you save the image, which is as good a guess as any other profile when there is no color profile information at all in the original image. You need to upload an image with a color profile to see if the JPEG encoder handles that correctly or not. As long as there is no color profile, there is no right or wrong when it comes to interpreting the color values in the image.

于 2013-10-13T01:49:32.083 回答