/// <summary>
 /// 调整大小
 /// </summary>
 /// <param name="image"></param>
 /// <param name="width">宽</param>
 /// <param name="height">高</param>
 /// <param name="zoom">等比缩放</param>
 /// <returns></returns>
 public static Image Resize(this Image image, int? width, int? height, bool zoom = true)
 {
     if (width == null && height == null)
         return image;
     int w = image.Width, h = image.Height;
     if (width != null && height != null)
     {
         double wW = (double)image.Width / width.Value, hH = (double)image.Height / height.Value;
         if (zoom)
         {
             if (wW > hH)
             {
                 w = width.Value;
                 h = (int)(image.Height / wW);
             }
             else
             {
                 w = (int)(image.Width / hH);
                 h = height.Value;
             }
         }
         else
         {
             w = width.Value;
             h = height.Value;
         }
     }
     else if (width != null)
     {
         w = width.Value;
         h = zoom ? (int)(image.Height / ((double)image.Width / width.Value)) : image.Height;
     }
     else if (height != null)
     {
         w = zoom ? (int)(image.Width / ((double)image.Height / height.Value)) : image.Width;
         h = height.Value;
     }
     if (image.Width == w && image.Height == h)
         return image;
     if (image.RawFormat.Guid == ImageFormat.Gif.Guid)
     {
         GifDecoder GifDecoder = new GifDecoder();
         GifDecoder.Read(image);
         int count = GifDecoder.FramesCount;
         AnimatedGifEncoder e = new AnimatedGifEncoder();
         MemoryStream ms = new MemoryStream();
         e.Start(ms);
         //int loopCount = 0;
         e.SetRepeat(GifDecoder.LoopCount);
         //int j = loopCount;
         for (int i = 0; i < count; i++)
         {
             Image frame = GifDecoder.GetFrame(i).GetThumbnailImage(w, h, () => { return false; }, IntPtr.Zero);
             e.SetDelay(GifDecoder.Delay(i));
             e.AddFrame(frame);
         }
         e.Finish();
         Image gif = Image.FromStream(ms);
         ms.Dispose();
         return gif;
     }
     else
     {
         Bitmap tmp1 = new Bitmap(image);
         Image tmp2 = tmp1.GetThumbnailImage(w, h, () => { return false; }, IntPtr.Zero);
         Bitmap result = new Bitmap(tmp2);
         image.Dispose();
         tmp1.Dispose();
         tmp2.Dispose();
         return result;
     }
 }
Exemple #2
0
        /// <summary>
        /// 调整大小
        /// </summary>
        /// <param name="image"></param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        /// <param name="zoom">等比缩放</param>
        /// <returns></returns>
        public static Image Resize(this Image image, int?width, int?height, bool zoom = true)
        {
            if (width == null && height == null)
            {
                return(image);
            }
            int w = image.Width, h = image.Height;

            if (width != null && height != null)
            {
                double wW = (double)image.Width / width.Value, hH = (double)image.Height / height.Value;
                if (zoom)
                {
                    if (wW > hH)
                    {
                        w = width.Value;
                        h = (int)(image.Height / wW);
                    }
                    else
                    {
                        w = (int)(image.Width / hH);
                        h = height.Value;
                    }
                }
                else
                {
                    w = width.Value;
                    h = height.Value;
                }
            }
            else if (width != null)
            {
                w = width.Value;
                h = zoom ? (int)(image.Height / ((double)image.Width / width.Value)) : image.Height;
            }
            else if (height != null)
            {
                w = zoom ? (int)(image.Width / ((double)image.Height / height.Value)) : image.Width;
                h = height.Value;
            }
            if (image.Width == w && image.Height == h)
            {
                return(image);
            }
            if (image.RawFormat.Guid == ImageFormat.Gif.Guid)
            {
                GifDecoder GifDecoder = new GifDecoder();
                GifDecoder.Read(image);
                int count             = GifDecoder.FramesCount;
                AnimatedGifEncoder e  = new AnimatedGifEncoder();
                MemoryStream       ms = new MemoryStream();
                e.Start(ms);
                //int loopCount = 0;
                e.SetRepeat(GifDecoder.LoopCount);
                //int j = loopCount;
                for (int i = 0; i < count; i++)
                {
                    Image frame = GifDecoder.GetFrame(i).GetThumbnailImage(w, h, () => { return(false); }, IntPtr.Zero);
                    e.SetDelay(GifDecoder.Delay(i));
                    e.AddFrame(frame);
                }
                e.Finish();
                Image gif = Image.FromStream(ms);
                ms.Dispose();
                return(gif);
            }
            else
            {
                Bitmap tmp1   = new Bitmap(image);
                Image  tmp2   = tmp1.GetThumbnailImage(w, h, () => { return(false); }, IntPtr.Zero);
                Bitmap result = new Bitmap(tmp2);
                image.Dispose();
                tmp1.Dispose();
                tmp2.Dispose();
                return(result);
            }
        }
 /// <summary>
 /// 裁剪图片
 /// </summary>
 /// <param name="image">源图片</param>
 /// <param name="startX">裁剪开始 X 坐标</param>
 /// <param name="startY">裁剪开始 Y 坐标</param>
 /// <param name="width">裁剪宽度</param>
 /// <param name="height">裁剪高度</param>
 /// <returns></returns>
 public static Image Crop(this Image image, int startX, int startY, int width, int height)
 {
     if (startX >= image.Width || startY >= image.Height || width <= 0 || height <= 0)
         return image;
     if (startX < 0)
         startX = 0;
     if (startY < 0)
         startY = 0;
     if (width > image.Width - startX)
         width = image.Width - startX;
     if (height > image.Height - startY)
         height = image.Height - startY;
     if (image.RawFormat.Guid == ImageFormat.Gif.Guid)
     {
         int w = image.Width;
         int h = image.Height;
         GifDecoder GifDecoder = new GifDecoder();
         GifDecoder.Read(image);
         int count = GifDecoder.FramesCount;
         AnimatedGifEncoder age = new AnimatedGifEncoder();
         MemoryStream ms = new MemoryStream();
         age.Start(ms);
         age.SetRepeat(GifDecoder.LoopCount);
         for (int i = 0; i < count; i++)
         {
             Image frame = new Bitmap(width, height);
             Graphics g = Graphics.FromImage(frame);
             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
             g.Clear(Color.Transparent); //清空画布并以透明背景色填充
             g.DrawImage(GifDecoder.GetFrame(i), -startX, -startY, w, h);
             g.Dispose();
             age.SetDelay(GifDecoder.Delay(i));
             age.AddFrame(frame);
         }
         age.Finish();
         return Image.FromStream(ms);
     }
     else
     {
         Image @new = new Bitmap(width, height);
         Graphics g = Graphics.FromImage(@new);
         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
         g.Clear(Color.Transparent); //清空画布并以透明背景色填充
         g.DrawImage(image, -startX, -startY, image.Width, image.Height);
         g.Dispose();
         return @new;
     }
 }