C#处理图片缩略图演示
时间:2014-07-23 08:09 来源: 我爱IT技术网 作者:山风
System.Drawing namespace 提供对 GDI+ 基本绘图功能的存取,可以方便处理图片的操作,下面是一个处理图片缩略图的范例:
- //using System.Drawing;
- var image = Image.FromFile(@"D:\001.jpg");
- int width = 120, height = 100;
- float targetRatio = (float)width / (float)height;
- float imageRatio = (float)image.Width / (float)image.Height;
- if(imageRatio < targetRatio)
- {
- width = Math.Max(1, height * image.Width / image.Height);
- }
- else
- {
- height = Math.Max(1, width * image.Height / image.Width);
- }
- var thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero);
- thumbnail.Save(@"D:\thumb.jpg");
这个方式可以容易的做到图片缩略图,但使用这个方式会很占用内存,在处理尺寸大的图片时有可能会出现 out of memory,而且在 MSDN 有以下的警示:
Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. For a supported alternative, see Windows Imaging Components.
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
