本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《 阿里云开发者社区用户服务协议 》和 《 阿里云开发者社区知识产权保护指引 》。如果您发现本社区中有涉嫌抄袭的内容,填写 侵权投诉表单 进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。 在网站开发时,生成缩略图是一个非常常见和实用的功能.以前在asp里只能借助com组件实现,现在在.net里可以利用框架的强大的类库轻松实现.下面帖出完整的代码(带详细注释),参考了网上的一些文章及.net sdk相关内容.QQROOM网络家园的图片上传用到了所有的4种生成方式.
None.gif
ExpandedBlockStart.gif
/// <summary>
InBlock.gif
/// 生成缩略图
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="originalImagePath"> 源图路径(物理路径) </param>
InBlock.gif
/// <param name="thumbnailPath"> 缩略图路径(物理路径) </param>
InBlock.gif
/// <param name="width"> 缩略图宽度 </param>
InBlock.gif
/// <param name="height"> 缩略图高度 </param>
ExpandedBlockEnd.gif
/// <param name="mode"> 生成缩略图的方式 </param> public static MakeThumbnail( string originalImagePath, string thumbnailPath, width, height, string mode)
ExpandedBlockStart.gif
{
InBlock.gif Image originalImage
= Image.FromFile(originalImagePath);
InBlock.gif
InBlock.gif
int towidth = width;
InBlock.gif
int toheight = height;
InBlock.gif
InBlock.gif
int x = 0 ;
InBlock.gif
int y = 0 ;
InBlock.gif
int ow = originalImage.Width;
InBlock.gif
int oh = originalImage.Height;
InBlock.gif
InBlock.gif
switch (mode)
ExpandedSubBlockStart.gif
{
InBlock.gif
case " HW " : // 指定高宽缩放(可能变形)
InBlock.gif
break ;
InBlock.gif
case " W " : // 指定宽,高按比例
InBlock.gif
toheight = originalImage.Height * width / originalImage.Width;
InBlock.gif
break ;
InBlock.gif
case " H " : // 指定高,宽按比例
InBlock.gif
towidth = originalImage.Width * height / originalImage.Height;
InBlock.gif
break ;
InBlock.gif
case " Cut " : // 指定高宽裁减(不变形)
InBlock.gif
if (( double )originalImage.Width / ( double )originalImage.Height > ( double )towidth / ( double )toheight)
ExpandedSubBlockStart.gif
{
InBlock.gif oh
= originalImage.Height;
InBlock.gif ow
= originalImage.Height * towidth / toheight;
InBlock.gif y
= 0 ;
InBlock.gif x
= (originalImage.Width - ow) / 2 ;
ExpandedSubBlockEnd.gif }

InBlock.gif
else
ExpandedSubBlockStart.gif
{
InBlock.gif ow
= originalImage.Width;
InBlock.gif oh
= originalImage.Width * height / towidth;
InBlock.gif x
= 0 ;
InBlock.gif y
= (originalImage.Height - oh) / 2 ;
ExpandedSubBlockEnd.gif }

InBlock.gif
break ;
InBlock.gif
default :
InBlock.gif
break ;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
// 新建一个bmp图片
InBlock.gif
Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
InBlock.gif
InBlock.gif
// 新建一个画板
InBlock.gif
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
InBlock.gif
InBlock.gif
// 设置高质量插值法
InBlock.gif
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
InBlock.gif
InBlock.gif
// 设置高质量,低速度呈现平滑程度
InBlock.gif
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
InBlock.gif
InBlock.gif
// 清空画布并以透明背景色填充
InBlock.gif
g.Clear(Color.Transparent);
InBlock.gif
InBlock.gif
// 在指定位置并且按指定大小绘制原图片的指定部分
InBlock.gif
g.DrawImage(originalImage, new Rectangle( 0 , 0 , towidth, toheight),
InBlock.gif
new Rectangle(x, y, ow,oh),
InBlock.gif GraphicsUnit.Pixel);
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gif
{
InBlock.gif
// 以jpg格式保存缩略图
InBlock.gif
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
ExpandedSubBlockEnd.gif }

InBlock.gif
catch (System.Exception e)
ExpandedSubBlockStart.gif
{
InBlock.gif
throw e;
ExpandedSubBlockEnd.gif }

InBlock.gif
finally
ExpandedSubBlockStart.gif
{
InBlock.gif originalImage.Dispose();
InBlock.gif bitmap.Dispose();
InBlock.gif g.Dispose();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif }

None.gif
None.gif 关键方法Graphics.DrawImage见ms
help: MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm