代码之家  ›  专栏  ›  技术社区  ›  Tom Gullen

ASP.net图像缩略图变灰(浅)-奇怪!

  •  1
  • Tom Gullen  · 技术社区  · 13 年前

    alt text

    这将调整大小以使其看起来:

    alt text

    存储在服务器上的所有图像都是正确的,背景为蓝色渐变。但当它的大小和服务显示与黑色背景!变得相当黑暗。

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

    我的缩略图代码是:

    <%@ 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;
            }
        }
    }
    
    3 回复  |  直到 13 年前
        1
  •  1
  •   Chris    13 年前

    考虑到这似乎是一个显示问题,然后我从经验中发现,PNG存储的东西,你通常可能不希望他们有很多用途。此缩略图包含与颜色空间和其他类似内容相关的数据块。众所周知,它们会把事情搞得一团糟。我想它们很适合拍照,但当你在网上工作时,如果你想把PNG中的颜色和网页中的HTML颜色匹配起来,它们会让你做噩梦。。。

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

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

        2
  •  1
  •   Aristos    13 年前

    我首先看到的是你没有处理位图。

        3
  •  1
  •   Dekker500    13 年前

    一、 同样,也可以看到大/小版本没有问题。