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

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using DomainObjects;
using DAO;
using Entities;
using System.IO;
using DomainObjects.VehicleDomainObjects;


public class DisplayImage : IHttpHandler
{
public void ProcessRequest (HttpContext context)

{
int i_height = 100;
int i_width = 100;
int m_height = 0;
int m_width = 0;

if (context.Request.QueryString["iheight"] != null)

{
i_height = Int32.Parse(Encryption.Decrypt(context.Request.QueryString["iheight"].ToString()));
}
if (context.Request.QueryString["iwidth"] != null)

{
i_width = Int32.Parse(Encryption.Decrypt(context.Request.QueryString["iwidth"].ToString()));
}
if (context.Request.QueryString["mheight"] != null)

{
m_height = Int32.Parse(Encryption.Decrypt(context.Request.QueryString["mheight"].ToString()));

}
if (context.Request.QueryString["mwidth"] != null)

{
m_width = Int32.Parse(Encryption.Decrypt(context.Request.QueryString["mwidth"].ToString()));

}

if (context.Request.QueryString["type"] != null && context.Request.QueryString["type"] != "")

{
switch (Encryption.Decrypt(context.Request.QueryString["type"]))

{
case "Vehicle":

if ((context.Request.QueryString["id"] != null && context.Request.QueryString["id"] != ""))

{
VehicleDO vDO = new VehicleDO();
byte[] logo = vDO.retrieveImage(Convert.ToInt32(Encryption.Decrypt(context.Request.QueryString["id"])));

if (logo != null && logo.Length != 32144 && logo.Length > 1000)

{
context.Response.ContentType = "image/jpeg";
System.Drawing.Image _image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(logo));

if (context.Request.QueryString["thumb"] != null && Encryption.Decrypt(context.Request.QueryString["thumb"]) == "y")

{
i_height = CalculateNewSize(m_height, m_width, _image.Height, _image.Width, "height");
i_width = CalculateNewSize(m_height, m_width, _image.Height, _image.Width, "width");
if (i_width < 100 || i_height < 100)

{
System.Drawing.Image _newimage = _image.GetThumbnailImage(i_width, i_height, null, new System.IntPtr());
_newimage.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else //Added for higher quality images

{
Bitmap b = (Bitmap)Bitmap.FromStream(new System.IO.MemoryStream(logo));
Bitmap output = new Bitmap(b, new Size(i_width, i_height));
System.Drawing.Image _newimage = output;
_newimage.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

}
else

{ _image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); }
}
else

{
WriteBlankImage(context, i_height, i_width, m_height, m_width);
}
}
else

{
WriteInvisibleImage(context);
}
break;
default:
break;
}
}
else if (context.Request.QueryString["Path"] != null)

{
FileStream file = new FileStream(context.Server.MapPath(Encryption.Decrypt(context.Request.QueryString["Path"])), FileMode.Open, FileAccess.Read);
byte[] logo = new byte[file.Length];

file.Read(logo, 0, Convert.ToInt32(file.Length));

if (logo != null && logo.Length != 32144 && logo.Length > 1000)

{
context.Response.ContentType = "image/jpeg";
System.Drawing.Image _image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(logo));

if (context.Request.QueryString["thumb"] != null && Encryption.Decrypt(context.Request.QueryString["thumb"]) == "y")

{
i_height = CalculateNewSize(m_height, m_width, _image.Height, _image.Width, "height");
i_width = CalculateNewSize(m_height, m_width, _image.Height, _image.Width, "width");
if (i_width < 100 || i_height < 100)

{
System.Drawing.Image _newimage = _image.GetThumbnailImage(i_width, i_height, null, new System.IntPtr());
_newimage.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else //Added for higher quality images

{
Bitmap b = (Bitmap)Bitmap.FromStream(new System.IO.MemoryStream(logo));
Bitmap output = new Bitmap(b, new Size(i_width, i_height));
System.Drawing.Image _newimage = output;
_newimage.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

}
else

{ _image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); }
}
else

{
WriteBlankImage(context, i_height, i_width, m_height, m_width);
}
file.Close();
}
else

{
WriteInvisibleImage(context);
}
}
protected void WriteBlankImage(HttpContext context, int i_height, int i_width, int m_height, int m_width)

{
// read in noimage.gif


FileStream file = new FileStream(context.Server.MapPath("Images/noimage.jpg"), FileMode.Open, FileAccess.Read);
byte[] logo = new byte[file.Length];

file.Read(logo, 0, Convert.ToInt32(file.Length));

//byte[] logo = sr.ReadBytes(Convert.ToInt32(file.Length));

// write out the image
System.Drawing.Image _image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(logo));
i_height = CalculateNewSize(m_height, m_width, _image.Height, _image.Width, "height");
i_width = CalculateNewSize(m_height, m_width, _image.Height, _image.Width, "width");
System.Drawing.Image _newimage = _image.GetThumbnailImage(i_width, i_height, null, new System.IntPtr());
context.Response.Clear();
_newimage.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//context.Response.OutputStream.Write(logo, 0, logo.Length);
context.Response.End();
file.Close();
}

protected void WriteInvisibleImage(HttpContext context)

{
FileStream file = new FileStream(context.Server.MapPath("Images/invisible.gif"), FileMode.Open, FileAccess.Read);
byte[] logo = new byte[file.Length];

file.Read(logo, 0, Convert.ToInt32(file.Length));

//byte[] logo = sr.ReadBytes(Convert.ToInt32(file.Length));

// write out the image

context.Response.Clear();
context.Response.OutputStream.Write(logo, 0, logo.Length);
context.Response.End();
file.Close();
}

/**//// <summary>
/// Grabs the best available size, maintaining aspect ratio, for an image within certain limits
/// </summary>
/// <param name="m_height">Maximum Height Allowed</param>
/// <param name="m_width">Maxiumum Width Allowed</param>
/// <param name="o_height">Original Image Height</param>
/// <param name="o_width">Original Image Width</param>
/// <param name="type">The type you want returned (height or width)</param>
/// <returns></returns>
protected int CalculateNewSize(int m_height, int m_width, int o_height, int o_width, string type)

{
int i_width = 0;
int i_height = 0;

if (o_height > m_height || o_width > m_width)

{


if (m_height != 0 && m_width != 0)

{

i_width = Convert.ToInt32(o_width / (Convert.ToDouble(o_height) / m_height));
i_height = Convert.ToInt32(o_height / (Convert.ToDouble(o_width) / m_width));
if (i_width > m_width)

{
i_height = Convert.ToInt32(o_height / (Convert.ToDouble(o_width) / m_width));
i_width = Convert.ToInt32(o_width / (Convert.ToDouble(o_height) / i_height));

}
if (i_height > m_height)

{
i_width = Convert.ToInt32(o_width / (Convert.ToDouble(o_height) / m_height));
i_height = Convert.ToInt32(o_height / (Convert.ToDouble(o_width) / i_width));
}
}
else

{
if (m_height != 0)

{
i_width = Convert.ToInt32(o_width / (Convert.ToDouble(o_height) / i_height));
}
if (m_width != 0)

{
i_height = Convert.ToInt32(o_height / (Convert.ToDouble(o_width) / i_width));
}
}
}
else

{
i_height = o_height;
i_width = o_width;
}
switch (type)

{
case "height":
return i_height;
break;
case "width":
return i_width;
break;
default:
return 0;
break;
}
}

public bool IsReusable
{

get
{
return false;
}
}

}
posted @
2008-05-07 22:46
N/A2011
阅读(
432)
评论()
收藏
举报