缩略图的创建

 

 

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_draw2\1.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");

 

 

参考链接

https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.image.getthumbnailimage?redirectedfrom=MSDN&view=netframework-4.7.2#System_Drawing_Image_GetThumbnailImage_System_Int32_System_Int32_System_Drawing_Image_GetThumbnailImageAbort_System_IntPtr_

https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.graphics.drawimage?view=netframework-4.7.2

 

posted @ 2019-02-22 12:23  JeffreyGoogle  阅读(269)  评论(0编辑  收藏  举报