动态生成缩略图

(一)

/// <summary>
/// 生成缩略图代码
/// </summary>
/// <param name="originalBmp">为原图类型  可以根据它来表示原图路径</param>
/// <param name="desiredWidth">为生成后的图片宽度</param>
/// <param name="desiredHeight">为生成后的图片高度</param>
/// <returns></returns>
public Bitmap CreateThumbnail(Bitmap originalBmp, int desiredWidth, int desiredHeight)
{
// If the image is smaller than a thumbnail just return it
if (originalBmp.Width <= desiredWidth && originalBmp.Height <= desiredHeight)
{
return originalBmp;
}
int newWidth, newHeight;
// scale down the smaller dimension
if ((decimal)desiredWidth / originalBmp.Width < (decimal)desiredHeight / originalBmp.Height)
{
decimal desiredRatio = (decimal)desiredWidth / originalBmp.Width;
newWidth = desiredWidth;
newHeight = (int)(originalBmp.Height * desiredRatio);
}
else
{
decimal desiredRatio = (decimal)desiredHeight / originalBmp.Height;
newHeight = desiredHeight;
newWidth = (int)(originalBmp.Width * desiredRatio);
}
// This code creates cleaner (though bigger) thumbnails and properly
// and handles GIF files better by generating a white background for
// transparent images (as opposed to black)
// This is preferred to calling Bitmap.GetThumbnailImage()
Bitmap bmpOut = new Bitmap(newWidth, newHeight);
using (Graphics graphics = Graphics.FromImage(bmpOut))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);
}
return bmpOut;
}

 

//当按下生成缩略图后触发事件
protected void Button1_Click(object sender, EventArgs e)
{
Bitmap BTM = new Bitmap(Server.MapPath("images/1.jpg"));
CreateThumbnail(BTM, 200, 80).Save(Server.MapPath("images/2.jpg"));
}

经验之谈:里面的图片全部用Bitmap类型来表示的   找到路径用server.mappath      

          这个方法有一个小小的bug就是它是根据比例来的   不是根据你确定的长宽的画布来的   所以虽然生成的图片不会走形但是比例也是一样的   不会自动填充

 

(二)

/// <summary>创建规定大小的图像    /// 源图像只能是JPG格式  
/// </summary> 
/// <param name="oPath">源图像绝对路径</param> 
/// <param name="tPath">生成图像绝对路径</param> 
/// <param name="width">生成图像的宽度</param> 
/// <param name="height">生成图像的高度</param> 
public void CreateImage(string oPath, string tPath, int width, int height)
{
Bitmap originalBmp = new Bitmap(oPath);
// 源图像在新图像中的位置
int left, top;
if (originalBmp.Width <= width && originalBmp.Height <= height)
{
// 原图像的宽度和高度都小于生成的图片大小  
left = (int)Math.Round((decimal)(width - originalBmp.Width) / 2);
top = (int)Math.Round((decimal)(height - originalBmp.Height) / 2);
// 最终生成的图像
Bitmap bmpOut = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(bmpOut))
{
// 设置高质量插值法    
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 清空画布并以白色背景色填充 
graphics.Clear(Color.White);
// 把源图画到新的画布上  
graphics.DrawImage(originalBmp, left, top, originalBmp.Width, originalBmp.Height);
}
bmpOut.Save(tPath);
bmpOut.Dispose();
return;
}
// 新图片的宽度和高度,如400*200的图像,想要生成160*120的图且不变形,
// 那么生成的图像应该是160*80,然后再把160*80的图像画到160*120的画布上 
int newWidth, newHeight;
if (width * originalBmp.Height < height * originalBmp.Width)
{
newWidth = width;
newHeight = (int)Math.Round((decimal)originalBmp.Height * width / originalBmp.Width);
// 缩放成宽度跟预定义的宽度相同的,即left=0,计算top 
left = 0;
top = (int)Math.Round((decimal)(height - newHeight) / 2);
}
else
{
newWidth = (int)Math.Round((decimal)originalBmp.Width * height / originalBmp.Height);
newHeight = height;
// 缩放成高度跟预定义的高度相同的,即top=0,计算left  
left = (int)Math.Round((decimal)(width - newWidth) / 2);
top = 0;
}
// 生成按比例缩放的图,如:160*80的图 
Bitmap bmpOut2 = new Bitmap(newWidth, newHeight);
using (Graphics graphics = Graphics.FromImage(bmpOut2))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);
}
// 再把该图画到预先定义的宽高的画布上,如160*120 
Bitmap lastbmp = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(lastbmp))
{
// 设置高质量插值法 
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 清空画布并以白色背景色填充 
graphics.Clear(Color.White);
// 把源图画到新的画布上  
graphics.DrawImage(bmpOut2, left, top);
}
lastbmp.Save(tPath);
lastbmp.Dispose();
}


 

//当按下生成缩略图后触发事件
protected void Button1_Click(object sender, EventArgs e)
{
CreateImage(Server.MapPath("images/1.jpg"), Server.MapPath("2.jpg"), 360, 150); 
}

经验之谈: 这个生成缩略图弥补了上面那种方法的不足



 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值