Mini Kabibi Habibi
<%@ WebHandler Language="C#" Class="Thumb" %>
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
public class Thumb : IHttpHandler {
public void ProcessRequest(HttpContext context) {
string photoName = context.Request.QueryString["i"];
int size = Convert.ToInt32(context.Request.QueryString["size"]);
string photoPath = context.Server.MapPath(System.IO.Path.Combine(FakeDataHelper.PhotosFolder, photoName));
if(File.Exists(photoPath)) {
using(Bitmap thumb = ChangeImageSize(Image.FromFile(photoPath), size)) {
byte[] buffer;
using(MemoryStream ms = new MemoryStream()) {
thumb.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
buffer = ms.ToArray();
}
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(buffer);
}
}
}
public bool IsReusable { get { return true; } }
Bitmap ChangeImageSize(Image original, int width) {
int newWidth = width;
int newHeight = (int)(original.Height * ((double)width / original.Width));
Bitmap thumbnail = new Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage(thumbnail);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(original, 0, 0, newWidth, newHeight);
return thumbnail;
}
}