privateSizeNewSize(intmaxWidth,intmaxHeight,intwidth,intheight)
{
doublew=0.0;
doubleh=0.0;
doublesw=Convert.ToDouble(width);
doublesh=Convert.ToDouble(height);
doublemw=Convert.ToDouble(maxWidth);
doublemh=Convert.ToDouble(maxHeight);
if(sw<mw&&sh<mh)
{
w=sw;
h=sh;
}
elseif((sw/sh)>(mw/mh))
{
w=maxWidth;
h=(w*sh)/sw;
}
else
{
h=maxHeight;
w=(h*sw)/sh;
}
returnnewSize(Convert.ToInt32(w),Convert.ToInt32(h));
}
privatevoidSendSmallImage(stringfileName,intmaxWidth,intmaxHeight)
{
System.Drawing.Imageimg=System.Drawing.Image.FromFile(Server.MapPath(fileName));
System.Drawing.Imaging.ImageFormatthisFormat=img.RawFormat;
SizenewSize=NewSize(maxWidth,maxHeight,img.Width,img.Height);
BitmapoutBmp=newBitmap(newSize.Width,newSize.Height);
Graphicsg=Graphics.FromImage(outBmp);
//设置画布的描绘质量
g.CompositingQuality=CompositingQuality.HighQuality;
g.SmoothingMode=SmoothingMode.HighQuality;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.DrawImage(img,newRectangle(0,0,newSize.Width,newSize.Height),
0,0,img.Width,img.Height,GraphicsUnit.Pixel);
g.Dispose();
if(thisFormat.Equals(ImageFormat.Gif))
{
Response.ContentType="image/gif";
}
else
{
Response.ContentType="image/jpeg";
}
//以下代码为保存图片时,设置压缩质量
EncoderParametersencoderParams=newEncoderParameters();
long[]quality=newlong[1];
quality[0]=100;
EncoderParameterencoderParam=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,quality);
encoderParams.Param[0]=encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo对象。
ImageCodecInfo[]arrayICI=ImageCodecInfo.GetImageEncoders();
ImageCodecInfojpegICI=null;
for(intx=0;x<arrayICI.Length;x++)
{
if(arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI=arrayICI[x];//设置JPEG编码
break;
}
}
if(jpegICI!=null)
{
outBmp.Save(Response.OutputStream,jpegICI,encoderParams);
}
else
{
outBmp.Save(Response.OutputStream,thisFormat);
}
img.Dispose();
outBmp.Dispose();
}