C#生成图片缩略图(2种思路)

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

前言:在日常图片浏览中,如果图片过多,只有一张张的打开图片才能知道图片的内容,显然这样浏览起来非常不便。Windows系统在浏览图片时提供了缩略图的功能,这样大大的方便了浏览者了解每张图片的内容,本实例采用两种方式实现了与Windows系统缩略图相同的功能。

第一种:生成MakeThumbnail方法

[flash=500,400]http://s1.knowsky.com/20160211/yz4mdjux4cq28.gif[/flash][flash=500,400]http://s1.knowsky.com/20160211/csvix324skl28.gif[/flash]1////<summary>2///生成缩略图3///</summary>4///<param name="originalImagePath">源图路径(物理路径)</param>5///<param name="thumbnailPath">缩略图路径(物理路径)</param>6///<param name="width">缩略图宽度</param>7///<param name="height">缩略图高度</param>8///<param name="mode">生成缩略图的方式</param>9publicstaticvoidMakeThumbnail(stringoriginalImagePath,stringthumbnailPath,intwidth,intheight,stringmode)10{11Image originalImage =Image.FromFile(originalImagePath);1213inttowidth =width;14inttoheight =height;1516intx =0;17inty =0;18intow =originalImage.Width;19intoh =originalImage.Height;2021switch(mode)22{23case"HW"://指定高宽缩放(可能变形)24

break;25case"W"://指定宽,高按比例26toheight = originalImage.Height * width/originalImage.Width;27

break;28case"H"://指定高,宽按比例29towidth = originalImage.Width * height/originalImage.Height;30

break;31case"Cut"://指定高宽裁减(不变形)32if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)33{34oh =originalImage.Height;35ow = originalImage.Height*towidth/toheight;36y =0;37x = (originalImage.Width - ow)/2;38}39else40{41ow =originalImage.Width;42oh = originalImage.Width*height/towidth;43x =0;44y = (originalImage.Height - oh)/2;45}46

break;47default:48

break;49}5051//新建一个bmp图片52Image bitmap =newSystem.Drawing.Bitmap(towidth,toheight);5354//新建一个画板55Graphics g =System.Drawing.Graphics.FromImage(bitmap);5657//设置高质量插值法58g.InterpolationMode =System.Drawing.Drawing2D.InterpolationMode.High;5960//设置高质量,低速度呈现平滑程度61g.SmoothingMode =System.Drawing.Drawing2D.SmoothingMode.HighQuality;6263//清空画布并以透明背景色填充64g.Clear(Color.Transparent);6566//在指定位置并且按指定大小绘制原图片的指定部分67g.DrawImage(originalImage,newRectangle(0,0, towidth, toheight),newRectangle(x, y, ow,oh),GraphicsUnit.Pixel);68try69{70//以jpg格式保存缩略图71bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);72}73catch(System.Exception e)74{75throwe;76}77finally78{79originalImage.Dispose();80bitmap.Dispose();81g.Dispose();82}83}8485ThumbnailClass

MakeThumbnail

第二种:写成ThumbnailClass类,书写4个重载方法,有直接返回Image对象的,有生成缩略图,并且保存到指定目录等等

[flash=500,400]http://s1.knowsky.com/20160211/yz4mdjux4cq28.gif[/flash][flash=500,400]http://s1.knowsky.com/20160211/csvix324skl28.gif[/flash]1usingSystem.IO;2usingSystem.Drawing;3usingSystem.Drawing.Imaging;45///<summary>6///图片处理类7///1、生成缩略图片或按照比例改变图片的大小和画质8///2、将生成的缩略图放到指定的目录下9///</summary>10publicclassImageClass11{12publicImage ResourceImage;13PRivateintImageWidth;14privateintImageHeight;1516publicstringErrMessage;1718///<summary>19///类的构造函数20///</summary>21///<param name="ImageFileName">图片文件的全路径名称</param>22publicImageClass(stringImageFileName)23{24ResourceImage=Image.FromFile(ImageFileName);25ErrMessage="";26}2728publicboolThumbnailCallback()29{30returnfalse;31}3233///<summary>34///生成缩略图重载方法1,返回缩略图的Image对象35///</summary>36///<param name="Width">缩略图的宽度</param>37///<param name="Height">缩略图的高度</param>38///<returns>缩略图的Image对象</returns>39publicImage GetReducedImage(intWidth,intHeight)40{41try42{43Image ReducedImage;4445Image.GetThumbnailImageAbort callb=newImage.GetThumbnailImageAbort(ThumbnailCallback);4647ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero);4849returnReducedImage;50}51catch(Exception e)52{53ErrMessage=e.Message;54returnnull;55}56}5758///<summary>59///生成缩略图重载方法2,将缩略图文件保存到指定的路径60///</summary>61///<param name="Width">缩略图的宽度</param>62///<param name="Height">缩略图的高度</param>63///<param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param>64///<returns>成功返回true,否则返回false</returns>65publicboolGetReducedImage(intWidth,intHeight,stringtargetFilePath)66{67try68{69Image ReducedImage;7071Image.GetThumbnailImageAbort callb=newImage.GetThumbnailImageAbort(ThumbnailCallback);7273ReducedImage=ResourceImage.GetThumbnailImage(Width,Height,callb,IntPtr.Zero);74ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);7576ReducedImage.Dispose();7778returntrue;79}80catch(Exception e)81{82ErrMessage=e.Message;83returnfalse;84}85}8687///<summary>88///生成缩略图重载方法3,返回缩略图的Image对象89///</summary>90///<param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>91///<returns>缩略图的Image对象</returns>92publicImage GetReducedImage(doublePercent)93{94try95{96Image ReducedImage;9798Image.GetThumbnailImageAbort callb=newImage.GetThumbnailImageAbort(ThumbnailCallback);99100ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);101ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);102103ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);104105returnReducedImage;106}107catch(Exception e)108{109ErrMessage=e.Message;110returnnull;111}112}113114///<summary>115///生成缩略图重载方法4,返回缩略图的Image对象116///</summary>117///<param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>118///<param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param>119///<returns>成功返回true,否则返回false</returns>120publicboolGetReducedImage(doublePercent,stringtargetFilePath)121{122try123{124Image ReducedImage;125126Image.GetThumbnailImageAbort callb=newImage.GetThumbnailImageAbort(ThumbnailCallback);127128ImageWidth=Convert.ToInt32(ResourceImage.Width*Percent);129ImageHeight=Convert.ToInt32(ResourceImage.Width*Percent);130131ReducedImage=ResourceImage.GetThumbnailImage(ImageWidth,ImageHeight,callb,IntPtr.Zero);132133ReducedImage.Save(@targetFilePath,ImageFormat.Jpeg);134135ReducedImage.Dispose();136137returntrue;138}139catch(Exception e)140{141ErrMessage=e.Message;142returnfalse;143}144}145}

ThumbnailClass

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航