我现在在服务器上存放了一张大小不一定的jpg图片,我现在把他下载下来的时候,需要动态控制它的大小。也就是说我需要压缩下载的图片。换句话说就是上传时图片2048*1024,下载的时候我需要把他变成按比例压缩的大小,已让他不至于变形。请高手指点,谢谢。
解决方案 »
- 【求助】如何用asp.net开发"插件式系统"
- Extjs的combobox使用jsonstore获取asp.net后台数据
- 如何把ASP.NET wep 应用程序发布到公司里面的服务器上
- 配置错误,请前辈指点
- Js 效果的问题
- 怎样在网页上播放声音?
- 值得想想:如何在网页中用链接去触发一个事件?
- 在asp.net中页面提示对注册表项“Global”的访问被拒绝,如何解决???
- 数据导出的问题?分不多了 麻烦大家帮帮我 在线等
- 怎么保存远程图片到本地啊!重赏!!!
- 关于URL重写的问题,我有很多个需要重写的网址,不想都写在web.config文件中,请问还有什么其他的办法。
- 在textaera换行
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Steam.Core
{
public static class ImageDeal
{
/**//// <summary>
/// 缩略图,按高度和宽度来缩略
///http://www.cnblogs.com/pooeo/
/// </summary>
/// <param name="image"></param>
/// <param name="size"></param>
/// <returns></returns>
public static Image Scale(Image image, Size size)
{
return image.GetThumbnailImage(size.Width, size.Height, null, new IntPtr());
}
/**//// <summary>
/// 缩略图,按倍数来缩略
///http://www.cnblogs.com/pooeo/
/// </summary>
/// <param name="image">原图</param>
/// <param name="multiple">放大或缩小的倍数,负数表示缩小,正数表示放大</param>
/// <returns></returns>
public static Image Scale(Image image, Int32 multiple)
{
Int32 newWidth;
Int32 newHeight;
Int32 absMultiple = Math.Abs(multiple);
if (multiple == 0)
{
return image.Clone() as Image;
}
if (multiple < 0)
{
newWidth = image.Width / absMultiple;
newHeight = image.Height / absMultiple;
}
else
{
newWidth = image.Width * absMultiple;
newHeight = image.Height * absMultiple;
}
return image.GetThumbnailImage(newWidth, newHeight, null, new IntPtr());
}
/**//// <summary>
/// 固定宽度缩略
///http://www.cnblogs.com/pooeo/
/// </summary>
/// <param name="image"></param>
/// <param name="width"></param>
/// <returns></returns>
public static Image ScaleFixWidth(Image image, Int32 width)
{
Int32 newWidth = width;
Int32 newHeight;
Double tempMultiple = (Double)newWidth / (Double)image.Width;
newHeight = (Int32)(((Double)image.Height) * tempMultiple);
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics newGp = Graphics.FromImage(newImage))
{
newGp.CompositingQuality = CompositingQuality.HighQuality;
//设置高质量插值法
newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
newGp.Clear(Color.Transparent);
newGp.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
}
return newImage;
}
/**//// <summary>
/// 固定高度缩略
///http://www.cnblogs.com/pooeo/
/// </summary>
/// <param name="image"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Image ScaleFixHeight(Image image, Int32 height)
{
Int32 newWidth;
Int32 newHeight = height;
Double tempMultiple = (Double)newHeight / (Double)image.Height;
newWidth = (Int32)(((Double)image.Width) * tempMultiple);
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics newGp = Graphics.FromImage(newImage))
{
newGp.CompositingQuality = CompositingQuality.HighQuality;
//设置高质量插值法
newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
newGp.Clear(Color.Transparent);
newGp.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
}
return newImage;
}
/**//// <summary>
/// 裁减缩略,根据固定的高度和宽度
///http://www.cnblogs.com/pooeo/
/// </summary>
/// <param name="image"></param>
/// <param name="width"></param>
/// <param name="heigth"></param>
/// <returns></returns>
public static Image ScaleCut(Image image, Int32 width, Int32 height)
{
int x = 0;
int y = 0;
int ow = image.Width;
int oh = image.Height;
if (width >= ow && height >= oh)
{
return image;
}
//如果结果要比原来的宽
if (width > ow)
{
width = ow;
}
if (height > oh)
{
height = oh;
}
if ((double)image.Width / (double)image.Height > (double)width / (double)height)
{
oh = image.Height;
ow = image.Height * width / height;
y = 0;
x = (image.Width - ow) / 2;
}
else
{
ow = image.Width;
oh = image.Width * height / width;
x = 0;
y = (image.Height - oh) / 2;
}
Image newImage = new Bitmap(width, height);
using (Graphics newGp = Graphics.FromImage(newImage))
{
newGp.CompositingQuality = CompositingQuality.HighQuality;
//设置高质量插值法
newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
newGp.Clear(Color.Transparent);
newGp.DrawImage(image, new Rectangle(0, 0, width, height),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
}
return newImage;
}
/**//// <summary>
/// 生成缩略图
///http://www.cnblogs.com/pooeo/
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
/**//// <summary>
/// 打水印,在某一点
///http://www.cnblogs.com/pooeo/
/// </summary>
/// <param name="image"></param>
/// <param name="waterImagePath"></param>
/// <param name="p"></param>
public static void Makewater(Image image, String waterImagePath, Point p)
{
ImageDeal.Makewater(image, waterImagePath, p, ImagePosition.TopLeft);
}
public static void Makewater(Image image, String waterImagePath, Point p, ImagePosition imagePosition)
{
using (Image warterImage = Image.FromFile(waterImagePath))
{
using (Graphics newGp = Graphics.FromImage(image))
{
newGp.CompositingQuality = CompositingQuality.HighQuality;
//设置高质量插值法
newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
switch (imagePosition)
{
case ImagePosition.BottomLeft:
p.Y = image.Height - warterImage.Height - p.Y;
break;
case ImagePosition.TopRigth:
p.X = image.Width - warterImage.Width - p.X;
break;
case ImagePosition.BottomRight:
p.Y = image.Height - warterImage.Height - p.Y;
p.X = image.Width - warterImage.Width - p.X;
break;
}
newGp.DrawImage(warterImage, new Rectangle(p, new Size(warterImage.Width, warterImage.Height)));
}
}
}
public static void Makewater(Image image, String waterStr, Font font, Brush brush, Point p)
{
ImageDeal.Makewater(image, waterStr, font, brush, p, ImagePosition.TopLeft);
}
public static void Makewater(Image image, String waterStr, Font font, Brush brush, Point p, ImagePosition imagePosition)
{
using (Graphics newGp = Graphics.FromImage(image))
{
Int32 stringWidth;
Int32 stringHeight;
stringHeight = (int)font.Size;
stringWidth = (int)(((float)StringDeal.GetBitLength(waterStr) / (float)2) * (font.Size + 1));
newGp.CompositingQuality = CompositingQuality.HighQuality;
//设置高质量插值法
newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//文字抗锯齿
newGp.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
switch (imagePosition)
{
case ImagePosition.BottomLeft:
p.Y = image.Height - stringHeight - p.Y;
break;
case ImagePosition.TopRigth:
p.X = image.Width - stringWidth - p.X;
break;
case ImagePosition.BottomRight:
p.Y = image.Height - stringHeight - p.Y;
p.X = image.Width - stringWidth - p.X;
break;
}
newGp.DrawString(waterStr, font, brush, p);
}
}
/**//// <summary>
/// 高质量保存
///http://www.cnblogs.com/pooeo/
/// </summary>
/// <param name="image"></param>
/// <param name="path"></param>
public static void SaveQuality(Image image, String path)
{
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0];
myEncoder = Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, 100L); // 0-100
myEncoderParameters.Param[0] = myEncoderParameter;
try
{
image.Save(path, myImageCodecInfo, myEncoderParameters);
}
finally
{
myEncoderParameter.Dispose();
myEncoderParameters.Dispose();
}
}
}
public enum StringPosition
{
TopLeft,
BottomLeft
}
public enum ImagePosition
{
TopLeft,
BottomLeft,
BottomRight,
TopRigth
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
/// <summary>
/// MakeThumbnails 的摘要说明
/// </summary>
public class MakeThumbnails
{
public MakeThumbnails()
{
}
/// <summary>
/// 生成缩略图(最终图片固定大小,图片按比例缩小,并为缩略图加上边框,以jpg格式保存)
/// </summary>
/// <param name="sourceImg">原图片(物理路径)</param>
/// <param name="toPath">缩略图存放地址(物理路径,带文件名)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="backColor">如果图片按比例缩小后不能填充满缩略图,则使用此颜色填充(比如"#FFFFFF")</param>
/// <param name="borderColor">边框颜色(比如"#999999")</param>
public static void MakePic(string sourceImg, string toPath, int width, int height, string backColor, string borderColor)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(sourceImg);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
string mode;
if (ow < towidth && oh < toheight)//如果原图片大小小于指定的图片的宽度和高度
{
towidth = ow;
toheight = oh;
}
else
{
if (originalImage.Width / originalImage.Height >= width / height)
{
mode = "W";
}
else
{
mode = "H";
}
switch (mode)
{
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
default:
break;
}
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(width, height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以指定颜色填充
g.Clear(ColorTranslator.FromHtml(backColor));
//在指定位置并且按指定大小绘制原图片的指定部分
int top = (height - toheight) / 2;
int left = (width - towidth) / 2;
g.DrawImage(originalImage, new System.Drawing.Rectangle(left, top, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
Pen pen = new Pen(ColorTranslator.FromHtml(borderColor));
g.DrawRectangle(pen, 0, 0, width - 1, height - 1);
try
{
//以jpg格式保存缩略图
bitmap.Save(toPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
{ System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic); }
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
/// 按比例缩小图片,自动计算高度
public void SmallPic(string strOldPic, string strNewPic, int intWidth)
{ System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intHeight=(intWidth / objPic.Width) * objPic.Height;
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic); }
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
} /// 按比例缩小图片,自动计算宽度
public void SmallPic(string strOldPic, string strNewPic, int intHeight)
{ System.Drawing.Bitmap objPic,objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intWidth=(intHeight / objPic.Height) * objPic.Width;
objNewPic=new System.Drawing.Bitmap(objPic,intWidth,intHeight);
objNewPic.Save(strNewPic); }
catch(Exception exp){throw exp;}
finally
{
objPic=null;
objNewPic=null;
}
}
参考
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="M_OriginalImagePath">源图路径(物理路径)</param>
/// <param name="M_ThumbnailPath">缩略图路径(物理路径)</param>
/// <param name="M_Width">缩略图宽度</param>
/// <param name="M_Height">缩略图高度</param>
public static void MakeThumbnail(string M_OriginalImagePath, string M_ThumbnailPath, int M_Width, int M_Height)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(M_OriginalImagePath); int towidth = 0;
int toheight = 0;
if (originalImage.Width >= M_Width && originalImage.Height <= M_Height)
{
towidth = M_Width;
toheight = originalImage.Height;
} if (originalImage.Width <= M_Width && originalImage.Height >= M_Height)
{
towidth = originalImage.Width;
toheight = M_Height;
}
if (originalImage.Width >= M_Width && originalImage.Height >= M_Height)
{
towidth = M_Width;
toheight = M_Height;
}
if (originalImage.Width <= M_Width && originalImage.Height <= M_Height)
{
towidth = originalImage.Width;
toheight = originalImage.Height;
}
int x = 0;//左上角的x坐标
int y = 0;//左上角的y坐标
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, x, y, towidth, toheight);
try
{
//以jpg格式保存缩略图
bitmap.Save(M_ThumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}