Thread: CSS and Forums
View Single Post
  #17 (permalink)  
Old October 10th, 2007, 07:22 AM
jimibt jimibt is offline
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

ok, here it is:

<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Text;
using System.Net;
using System.Web.Caching;

public class ImageHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        bool succesfulImage = false;
        // Get the image ID from querystring, and use it to generate a cache key.
        String objectID = context.Request.QueryString["ObjectID"];
        //String cacheKey = context.Request.CurrentExecutionFilePath + ":" + objectID;
        String cacheKey = objectID;
        Byte[] imageBytes = null;

        if (objectID.Substring(objectID.LastIndexOf(".")) == ".flv")
        {
            context.Response.ContentType = "video/x-flv";
        }
        else
        {
            context.Response.ContentType = "image/jpeg";
        }

        // Check if the cache contains the image.
        Object cachedImageBytes = context.Cache.Get(cacheKey);
        if (cachedImageBytes != null)
        {
            try
            {
                imageBytes = (Byte[])cachedImageBytes;
                succesfulImage = true;
            }
            catch { succesfulImage = false; }
        }
        else
        {
            try
            {
                // Get image from uri (in ObjectID querystring),
                imageBytes = RequestGetMedia(objectID);
                // Store it in the cache (to be expired after 2 hours).
                // do the same cache.add in the file upload section
                // with the same key (i.e. the image url)
                // examine the cache timespan - is 2 hrs enough??
                context.Cache.Add(cacheKey, imageBytes, null,
                 DateTime.MaxValue, new TimeSpan(2, 0, 0),
                 CacheItemPriority.Normal, null);
                succesfulImage = true;
            }
            catch { succesfulImage = false; }
        }

        if (succesfulImage)
        {
            // Send back image.
            context.Response.Cache.SetCacheability(HttpCacheab ility.Public);
            context.Response.BufferOutput = true;
            context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    private byte[] RequestGetMedia(string TheURL)
    {
        Uri uri = new Uri(TheURL);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        Byte[] imageBytes = null;
        Encoding enc = Encoding.Default; //GetEncoding(1252); // Windows default Code Page

        try
        {
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version10;
            request.Method = "GET";
            request.Proxy = WebRequest.DefaultWebProxy;

            //allow auto redirects from redirect headers
            request.AllowAutoRedirect = true;

            //maximum of 10 auto redirects
            request.MaximumAutomaticRedirections = 10;

            //60 second timeout for request
            request.Timeout = (int)new TimeSpan(0, 0, 60).TotalMilliseconds;

            //give the crawler a name.
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(responseStream, enc);

            imageBytes = enc.GetBytes(readStream.ReadToEnd());
            responseStream.Dispose();
            readStream.Dispose();
        }
        catch (Exception ee)
        {
            // page = "Fail message : " + ee.Message;
        }
        return imageBytes;
    }
}

and to use it, just do this:

<img alt="a tooltip" src="ImageHandler.ashx?ObjectID=http://images.originaltalent.com/323cac00680947438e67f1d53bf648a5.JPG" />

or in code:

                        <div style="text-align: center">
                            <img src='<%# "ImageHandler.ashx?ObjectID=" + DataBinder.Eval(Container.DataItem,"ThumbURL") %>'
                                alt='' class="RotatorImage" onclick='showCallbackImage(this)' /><br />
                        </div>

good luck


jimi

http://www.originaltalent.com