ASP.net image thumbnails go greyscale(ish) - Weird!
Given an original image of:
This resizes to look as such:
ALL the images being stores on the server are correct with BLUE gradient background. But when it is resized and served it shows with black background! And darkened considerably.
On my local server there is no problem, it only does this on the live server!
My thumbnailing code is:
<%@ 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));
开发者_C百科 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;
}
}
}
Given that it seems to be a display problem then a few things I've found from experience is that PNGs store stuff that you usually probably don't want them to for a lot of purposes. This thumbnail contains chunks of data relating to colour spaces and other such things. These have been known to screw stuff around a fair bit. I assume they are good for photos but they can cause a nightmare when doing web work and trying to match a colour in the PNG with an HTML colour in the page...
Look here: http://the.earth.li/~chris/temp/tomgullenquestion_1XOA8.png
This is a copy of the thumbnail image with the non-critical chunks trimmed out so you can test if that is contributing to the problem or not.
The first this I see is that you did not dispose the BitMap.
I would strongly suggest you test by clearing your cache (CTRL-F5 in Chrome, for example). It could be that your image was (at one time) corrupted, and it is that corrupted version that is in the cache.
I, too, see no problems with the large/small version as presented.
精彩评论