using System.Drawing;
public class ThumbHelper { /// <summary> /// 请求大缩略图(例如300*300) /// </summary> /// <param name="sourceImage"></param> /// <param name="targetWidth"></param> /// <param name="targetHeight"></param> /// <returns></returns> public static Image ImageThumbProcess(Image sourceImage, int targetWidth, int targetHeight) { Bitmap targetPicture = new Bitmap(targetWidth, targetHeight); using (Graphics g = Graphics.FromImage(targetPicture)) { g.DrawImage(sourceImage, 0, 0, targetWidth, targetHeight); } return targetPicture; } /// <summary> /// 请求120*120尺寸的缩略图 /// </summary> /// <param name="sourceImage"></param> /// <param name="targetWidth"></param> /// <param name="targetHeight"></param> /// <returns></returns> public static Image GetThumbnailImage(Image sourceImage, int targetWidth, int targetHeight) { return sourceImage.GetThumbnailImage(targetWidth, targetHeight, () => false, IntPtr.Zero); } }
string img = @"E:DefctFramework数据count3_for_draw21.JPG"; Bitmap bitmap = new Bitmap(img); //小缩略图 Image smallThumb = bitmap.GetThumbnailImage(120, 120, () => false, IntPtr.Zero); smallThumb.Save(@"120.jpg"); Image smallThumbX = ThumbHelper.ImageThumbProcess(bitmap, 120, 120); smallThumbX.Save(@"120x.jpg"); //大缩略图 Image bigThumb = bitmap.GetThumbnailImage(256, 256, () => false, IntPtr.Zero); bigThumb.Save(@"256.jpg"); Image bigThumbX = ThumbHelper.ImageThumbProcess(bitmap, 256, 256); bigThumbX.Save(@"256x.jpg");
参考链接