【问题标题】:ASP.net image thumbnails go greyscale(ish) - Weird!ASP.net 图像缩略图变为灰度(ish) - 奇怪!
【发布时间】:2011-05-04 13:34:34
【问题描述】:

给定一个原始图像:

这会调整大小以使其看起来像这样:

服务器上存储的所有图像都正确,带有蓝色渐变背景。但是当它被调整大小并被送达时,它会以黑色背景显示!并且变暗了很多。

在我的本地服务器上没有问题,它只在实时服务器上这样做!

我的缩略图代码是:

<%@ WebHandler Language="C#" Class="Thumbnail" %>

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;

public class Thumbnail : IHttpHandler {

    private int _thumbnailSize = 150;

    public void ProcessRequest(HttpContext context) {

        // Name of photo file
        string photoName = context.Request.QueryString["p"];

        // Size index
        string sizeIndex = context.Request.QueryString["s"];
        string saveAction = context.Request.QueryString["a"];
        int width;
        int height;
        int maxWidth = 0;
        int maxHeight = 0;
        Bitmap photo;
        bool customResize = false;

        //Get original path of picture
        string photoPath = "";
        if (photoName.IndexOf('/') > 0)
        {
            photoPath = context.Server.MapPath(photoName);
        }
        else
        {

            photoPath = context.Server.MapPath("../uploads/originals/" + photoName);
        }

        // Create new bitmap
        try {
            photo = new Bitmap(photoPath);
        }
        catch (ArgumentException) {
            throw new HttpException(404, "Photo not found.");
        }
        context.Response.ContentType = "image/png";

        // Initialise width as native
        width = photo.Width;
        height = photo.Height;

        // Slideshow image (big)
        if (sizeIndex == "1")
        {
            // Set max widths and heights
            maxWidth = 500;
            maxHeight = 300;
            customResize = true;

        }
        // Big(ger) thumbnail
        else if (sizeIndex == "3")
        {
            // Set max widths and heights
            maxWidth = 150;
            maxHeight = 150;
            customResize = true;

        }
        // Big(ger) thumbnail
        else if (sizeIndex == "4")
        {
            // Set max widths and heights
            maxWidth = 30;
            maxHeight = 30;
            customResize = true;
        }
        // Standard thumbnail
        else
        {
            maxHeight = 75;

            // Normalise height
            if (photo.Height > maxHeight)
            {
                height = maxHeight;
                double newWidth = photo.Width / (photo.Height / height);
                width = int.Parse(newWidth.ToString());
            }
            else
            {
                height = photo.Height;
                width = photo.Width;
            }
        }

        // Resize
        if (customResize && (width > maxWidth || height > maxHeight))
        {

            double scale = Math.Min(1, Math.Min((double)maxWidth / (double)photo.Width, (double)maxHeight / (double)photo.Height));
            width = int.Parse((Math.Round((double)photo.Width * scale,0)).ToString());
            height = int.Parse((Math.Round((double)photo.Height * scale,0)).ToString());
        }

        // Generate and show image
        Bitmap target = new Bitmap(width, height);
        using (Graphics graphics = Graphics.FromImage(target)) {
            graphics.CompositingQuality = CompositingQuality.HighSpeed;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.DrawImage(photo, 0, 0, width, height);
            using (MemoryStream memoryStream = new MemoryStream()) {
                target.Save(memoryStream, ImageFormat.Png);
                //OutputCacheResponse(context, File.GetLastWriteTime(photoPath));
                //using (FileStream diskCacheStream = new FileStream(cachePath, FileMode.CreateNew)) {
                //    memoryStream.WriteTo(diskCacheStream);
                //}

                // If savinf
                if (saveAction == "s")
                {
                    FileStream outStream = File.OpenWrite(context.Server.MapPath("../uploads/gallery/" + photoName));
                    memoryStream.WriteTo(outStream);
                    outStream.Flush();
                    outStream.Close();
                }
                else{
                    memoryStream.WriteTo(context.Response.OutputStream);   
                }
            }
        }



    }

    private static void OutputCacheResponse(HttpContext context, DateTime lastModified) {
       /*   HttpCachePolicy cachePolicy = context.Response.Cache;
            cachePolicy.SetCacheability(HttpCacheability.Public);
            cachePolicy.VaryByParams["p"] = true;
            cachePolicy.SetOmitVaryStar(true);
            cachePolicy.SetExpires(DateTime.Now + TimeSpan.FromDays(7));
            cachePolicy.SetValidUntilExpires(true);
            cachePolicy.SetLastModified(lastModified);*/
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

【问题讨论】:

  • 另外我知道代码不是很好,修改了一些示例代码,这是很久以前的事了。
  • 该缩略图是否是缩略图损坏的示例?对我来说它看起来不错......(没有黑色背景等)。我想到的一个想法是这是否与透明度有关?如果您的缩略图正在调整大小,然后在没有 Alpha 通道的情况下输出并假设为黑色背景,我可以看到它可以按照您的描述进行......
  • 什么?两张的小图是不是和第一张颜色一样?!?!?!?!
  • @Tom Gullen - 图片中的颜色对我来说也完全一样
  • 从 png 图像中去除颜色配置文件实际上是相对简单的。 PNG旨在一次删除块,当我们提供大量PNG时,我们总是剥离此信息以获得较小的尺寸增益,并获得不会因任何原因改变颜色的标准图像的巨大增益.似乎很奇怪,在 .NET(我正在玩这个的 2.0)中创建一个没有颜色配置文件块的 PNG 几乎是不可能的......

标签: asp.net image-processing thumbnails ashx image-resizing


【解决方案1】:

鉴于这似乎是一个显示问题,那么我从经验中发现的一些事情是 PNG 存储的东西通常您可能出于很多目的不希望它们存储。此缩略图包含与色彩空间和其他此类事物相关的大量数据。众所周知,这些东西会乱七八糟。我认为它们对照片很有用,但在进行网络工作并尝试将 PNG 中的颜色与页面中的 HTML 颜色匹配时,它们可能会引发一场噩梦......

看这里:http://the.earth.li/~chris/temp/tomgullenquestion_1XOA8.png

这是缩略图的副本,其中非关键块已被修剪掉,因此您可以测试这是否导致问题。

【讨论】:

    【解决方案2】:

    我看到的第一个是你没有处理位图。

    【讨论】:

      【解决方案3】:

      我强烈建议您通过清除缓存来进行测试(例如,Chrome 中的 CTRL-F5)。可能是您的图像(曾经)损坏了,而缓存中的那个损坏的版本。

      我也认为所展示的大/小版本没有问题。

      【讨论】:

        猜你喜欢
        • 2023-03-16
        • 1970-01-01
        • 2010-10-03
        • 1970-01-01
        • 2013-07-28
        • 1970-01-01
        • 1970-01-01
        • 2019-03-08
        • 1970-01-01
        相关资源
        最近更新 更多