//需引用的命名空间
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Response.Clear() ;
try
{
//需显示的图片的物理路径
string originalFileName = Request["fn"].ToString();
//需显示的图片的宽度
int thumbnailWidth = Convert.ToInt32(Request["tw"]);
//需显示的图片的高度
int thumbnailHeight = Convert.ToInt32(Request["th"]);
System.Drawing.Image img = System.Drawing.Image.FromFile(originalFileName);
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat ;
System.Drawing.Size newSize = this.GetNewSize(thumbnailWidth,thumbnailHeight,img.Width,img.Height);
System.Drawing.Bitmap outBmp = new Bitmap(thumbnailWidth,thumbnailHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(outBmp);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality ;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear ;
g.Clear(System.Drawing.Color.FromArgb(255,249,255,240));
g.DrawImage(img,new Rectangle((thumbnailWidth-newSize.Width)/2,(thumbnailHeight-newSize.Height)/2,newSize.Width,newSize.Height),0,0,img.Width,img.Height,GraphicsUnit.Pixel);
g.Dispose();
if(thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
Response.ContentType = "image/gif";
else
Response.ContentType = "image.jpeg";
System.Drawing.Imaging.EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[] {100};
System.Drawing.Imaging.EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
for(int fwd=0;fwd<arrayICI.Length;fwd++)
{
if(arrayICI[fwd].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[fwd];
break;
}
}
if(jpegICI != null)
{
outBmp.Save(Response.OutputStream,jpegICI,encoderParams);
}
else
{
outBmp.Save(Response.OutputStream,thisFormat);
}
}
catch(Exception err)
{
}
}
private System.Drawing.Size GetNewSize(int maxWidth, int maxHeight, int width, int height)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(width);
double sh = Convert.ToDouble(height);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if(sw<mw && sh<mh)
{
w = sw;
h = sh;
}
else if((sw/sh)>(mw/mh))
{
w = maxWidth;
h = (w*sh)/sw;
}
else
{
h = maxHeight;
w = (h*sw)/sh;
}
return new System.Drawing.Size(Convert.ToInt32(w),Convert.ToInt32(h));
}