<%@ WebHandler Language="C#" Class="DisplayImage" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class DisplayImage : IHttpHandler
{
public void ProcessRequest (HttpContext context)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i_height = 100;
int i_width = 100;
int m_height = 0;
int m_width = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (context.Request.QueryString["iheight"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i_height = Int32.Parse(Encryption.Decrypt(context.Request.QueryString["iheight"].ToString()));
}
if (context.Request.QueryString["iwidth"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i_width = Int32.Parse(Encryption.Decrypt(context.Request.QueryString["iwidth"].ToString()));
}
if (context.Request.QueryString["mheight"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_height = Int32.Parse(Encryption.Decrypt(context.Request.QueryString["mheight"].ToString()));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
if (context.Request.QueryString["mwidth"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_width = Int32.Parse(Encryption.Decrypt(context.Request.QueryString["mwidth"].ToString()));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (context.Request.QueryString["type"] != null && context.Request.QueryString["type"] != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (Encryption.Decrypt(context.Request.QueryString["type"]))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "Vehicle":
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ((context.Request.QueryString["id"] != null && context.Request.QueryString["id"] != ""))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
VehicleDO vDO = new VehicleDO();
byte[] logo = vDO.retrieveImage(Convert.ToInt32(Encryption.Decrypt(context.Request.QueryString["id"])));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (logo != null && logo.Length != 32144 && logo.Length > 1000)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
context.Response.ContentType = "image/jpeg";
System.Drawing.Image _image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(logo));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (context.Request.QueryString["thumb"] != null && Encryption.Decrypt(context.Request.QueryString["thumb"]) == "y")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ _image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); }
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteBlankImage(context, i_height, i_width, m_height, m_width);
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteInvisibleImage(context);
}
break;
default:
break;
}
}
else if (context.Request.QueryString["Path"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileStream file = new FileStream(context.Server.MapPath(Encryption.Decrypt(context.Request.QueryString["Path"])), FileMode.Open, FileAccess.Read);
byte[] logo = new byte[file.Length];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
file.Read(logo, 0, Convert.ToInt32(file.Length));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (logo != null && logo.Length != 32144 && logo.Length > 1000)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
context.Response.ContentType = "image/jpeg";
System.Drawing.Image _image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(logo));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (context.Request.QueryString["thumb"] != null && Encryption.Decrypt(context.Request.QueryString["thumb"]) == "y")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ _image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); }
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteBlankImage(context, i_height, i_width, m_height, m_width);
}
file.Close();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteInvisibleImage(context);
}
}
protected void WriteBlankImage(HttpContext context, int i_height, int i_width, int m_height, int m_width)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// read in noimage.gif
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FileStream file = new FileStream(context.Server.MapPath("Images/noimage.jpg"), FileMode.Open, FileAccess.Read);
byte[] logo = new byte[file.Length];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
file.Read(logo, 0, Convert.ToInt32(file.Length));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//byte[] logo = sr.ReadBytes(Convert.ToInt32(file.Length));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 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();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void WriteInvisibleImage(HttpContext context)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileStream file = new FileStream(context.Server.MapPath("Images/invisible.gif"), FileMode.Open, FileAccess.Read);
byte[] logo = new byte[file.Length];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
file.Read(logo, 0, Convert.ToInt32(file.Length));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//byte[] logo = sr.ReadBytes(Convert.ToInt32(file.Length));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// write out the image
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
context.Response.Clear();
context.Response.OutputStream.Write(logo, 0, logo.Length);
context.Response.End();
file.Close();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i_width = 0;
int i_height = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (o_height > m_height || o_width > m_width)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (m_height != 0 && m_width != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i_height = Convert.ToInt32(o_height / (Convert.ToDouble(o_width) / m_width));
i_width = Convert.ToInt32(o_width / (Convert.ToDouble(o_height) / i_height));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
if (i_height > m_height)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (m_height != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i_width = Convert.ToInt32(o_width / (Convert.ToDouble(o_height) / i_height));
}
if (m_width != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i_height = Convert.ToInt32(o_height / (Convert.ToDouble(o_width) / i_width));
}
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i_height = o_height;
i_width = o_width;
}
switch (type)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "height":
return i_height;
break;
case "width":
return i_width;
break;
default:
return 0;
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public bool IsReusable
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return false;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
posted @
2008-05-07 22:46
N/A2011
阅读(
424)
评论()
编辑
收藏
举报