Исходное изображение:
Это изменяет размер, чтобы выглядеть так:
ВСЕ изображения, хранящиеся на сервере, имеют правильный синий фон с градиентом. Но когда он изменен и обслуживается, он показывает черный фон! И значительно потемнело.
На моем локальном сервере проблем нет, он делает это только на живом сервере!
Мой код миниатюры:
<%@ 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;
}
}
}