方法一
使用 C# 程序处理:
/// <summary>
/// 按 原图比例进行等比例缩放
/// </summary>
/// <param name="objImage">原始的图片</param>
/// <param name="thumbnailPath">新图片存放的地址</param>
/// <param name="cutWidth">指定宽度</param>
/// <param name="cutHeight">指定高度</param>
public static void CutImageCustom(Image objImage, string thumbnailPath, int cutWidth, int cutHeight)
{
int width;
int height;
float x = objImage.Width;
float y = objImage.Height;
float xPercent = x / cutWidth;
float yPercent = y / cutHeight;
if (xPercent < yPercent)
{
width = (int )((x * cutHeight) / y);
height = cutHeight;
}
else
{
width = cutWidth;
height = (int )((cutWidth * y) / x);
}
Bitmap newimage = new Bitmap (width,height, PixelFormat .Format32bppRgb);
newimage.SetResolution(72f, 72f);
Graphics gdiobj = Graphics .FromImage(newimage);
gdiobj.CompositingQuality = CompositingQuality .HighQuality;
gdiobj.SmoothingMode = SmoothingMode .HighQuality;
gdiobj.InterpolationMode = InterpolationMode .HighQualityBicubic;
gdiobj.PixelOffsetMode = PixelOffsetMode .HighQuality;
gdiobj.FillRectangle(new SolidBrush (Color .White), 0, 0,width,height);
Rectangle destrect = new Rectangle (0, 0,width,height);
gdiobj.DrawImage(objImage, destrect, 0, 0, objImage.Width, objImage.Height, GraphicsUnit .Pixel);
gdiobj.Dispose();
System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters (1);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter (System.Drawing.Imaging.Encoder .Quality, (long )100);
ImageCodecInfo [] codecs = ImageCodecInfo .GetImageEncoders();
ImageCodecInfo ici = null ;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg" ) { ici = codec; }
}
if (ici != null ) newimage.Save(thumbnailPath, ici, ep); else newimage.Save(thumbnailPath, objImage.RawFormat);
try
{
//将新生成的缩略图存放到指定的位置
newimage.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat .Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
objImage.Dispose();
newimage.Dispose();
gdiobj.Dispose();
}
}
// 调用方法
System.Drawing.Image originalImage = System.Drawing.Image .FromStream(file.InputStream);
ImageHelper .CutImageCustom(originalImage, thumbnailPath, 84, 84);
/// <summary>
/// 指定长度和高度和 指定从何处开始区域
/// </summary>
/// <param name="bmp">原始图像</param>
/// <param name="saveFilePath">缩略图存放的地址</param>
/// <param name="x">横坐标切起始位置</param>
/// <param name="y">纵坐标切起始位置</param>
/// <param name="width">指定宽度</param>
/// <param name="height">指定高度</param>
public static void CutAsJPG(Bitmap bmp, string saveFilePath, int x, int y, int width, int height)
{
int bmpW = bmp.Width;
int bmpH = bmp.Height;
if (x >= bmpW || y >= bmpH)
{
CompressAsJPG(bmp, saveFilePath, 80);
return ;
}
if (x + width > bmpW)
{
width = bmpW - x;
}
if (y + height > bmpH)
{
height = bmpH - y;
}
Bitmap bmpOut = new Bitmap (width, height, PixelFormat .Format24bppRgb);
Graphics g = Graphics .FromImage(bmpOut);
g.DrawImage(bmp, new Rectangle (0, 0, width, height), new Rectangle (x, y, width, height), GraphicsUnit .Pixel);
g.Dispose();
bmp.Dispose();
CompressAsJPG(bmpOut, saveFilePath, 80);
}
public static void CompressAsJPG(Bitmap bmp, string saveFilePath, int quality)
{
EncoderParameter p = new EncoderParameter (System.Drawing.Imaging.Encoder .Quality, quality); ;
EncoderParameters ps = new EncoderParameters (1);
ps.Param[0] = p;
bmp.Save(saveFilePath, GetImageCodecInfo("image/jpeg" ), ps);
bmp.Dispose();
}
private static ImageCodecInfo GetImageCodecInfo(string mimeType)
{
ImageCodecInfo [] CodecInfo = ImageCodecInfo .GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType) return ici;
}
return null ;
}
//System.Drawing.Image originalImage = System.Drawing.Image .FromStream(file.InputStream);
//ImageHelper.CutAsJPG(bitmap, thumbnailPath, 0, 0, 84, 84);
方法二:
Js 代码处理
< script language ="javascript" type ="text/javascript">
var proMaxHeight = 480;
var proMaxWidth = 480;
//<img src=" 图片 ?" border=0 width="180" height="110" οnlοad=" proDownImage (this);">
function proDownImage(ImgD) {
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0) {
var rate = (proMaxWidth / image.width < proMaxHeight / image.height) ? proMaxWidth / image.width : proMaxHeight / image.height;
if (rate <= 1) {
ImgD.width = image.width * rate;
ImgD.height = image.height * rate;
}
else {
ImgD.width = image.width;
ImgD.height = image.height;
}
}
}
</ script >