图片附件上传时太大,页面展示时加载缓慢,需要显示缩略图

手机拍的图片有时候会很大,上传上去的时候,展示加载会很慢,这时候我们就需要在上传的时候,同时生成缩略图了,在展示的时候加载缩略图就快很多了,在单独点击的时候,再展示原图

下面附上,上传时生成缩略图的方法

//获取图片的真实路径
string truefilepath = SysConfig.GetFileServerUpload(FilePath);
//设置缩略图的路径
string CopyPath = SysConfig.GetFileServerUpload(CopyPath );
//判断如果缩略图已有当前的路径,则删除
if (File.Exists(CopyPath))
{
	File.Delete(CopyPath);
}
Image originalImage = Image.FromFile(truefilepath);
int thumWidth = 600;      //缩略图的宽度
int thumHeight = 400;    //缩略图的高度
int originalWidth = originalImage.Width;    //原始图片的宽度
int originalHeight = originalImage.Height;  //原始图片的高度                          
Image bitmap = new Bitmap(thumWidth, thumHeight);//新建一个bmp图片                            
Graphics graphic = Graphics.FromImage(bitmap);//新建一个画板                            
graphic.InterpolationMode = InterpolationMode.High;//设置高质量查值法                            
graphic.SmoothingMode = SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度                            
graphic.Clear(Color.Transparent);//清空画布并以透明背景色填充                            
graphic.DrawImage(originalImage, new Rectangle(0, 0, thumWidth, thumHeight), new Rectangle(0, 0, originalWidth, originalHeight), GraphicsUnit.Pixel);//在指定位置并且按指定大小绘制原图片的指定部分
try
{
    bitmap.Save(CopyPath, ImageFormat.Jpeg);
}
catch (Exception ex)
{
     throw ex;
}
finally
 {
     originalImage.Dispose();
     bitmap.Dispose();
     graphic.Dispose();
}

这里缩略图的像素设置为600*400,大小为70K左右

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值