Create a new generic Handler in Visual Studio.
This is the Code For it:
<%@ WebHandler Language="C#" Class="ImageService" %>
using System;
using System.Web;
using Commerce.CaptchaImage;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Net;
using System.IO;
namespace SitesBytes.ImageHandler{
public class ImageService : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request["r"] != null)
ImageResize(context);
}
public void ImageResize(HttpContext context )
{
Image sourceImage = GetImageFromURL(/*PUT YOUR DOMAIN HERE*/ + context.Request["src"]);
double width = (context.Request["width"] != null) ? Convert.ToInt32(context.Request["width"]) : 100;
double sizeFactor = width / sourceImage.Width;
double newHeigth = sizeFactor * sourceImage.Height;
Bitmap newImage = new Bitmap((int)width, (int)newHeigth);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(sourceImage, new Rectangle(0, 0, (int)width, (int)newHeigth));
}
newImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
private static Image GetImageFromURL(string url)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
return Image.FromStream(stream);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
The URL to Call the handler is:
<img src="~/Imagehandler.ashx?r=~/images/test.jpg&w=200" alt="something" />
That means the Image is in domain.com/images/test.jpg
The resized width is 200
Didnt have the time to test it right now, but it should work.
Cheers
Timmey