zoukankan      html  css  js  c++  java
  • C# 实现图片压缩

    代码:

    private static ImageCodecInfo GetImageCodecInfo(ImageFormat imageFormat)
    {
        ImageCodecInfo[] imageCodecInfoArr = ImageCodecInfo.GetImageDecoders();
        foreach (ImageCodecInfo imageCodecInfo in imageCodecInfoArr)
        {
            if (imageCodecInfo.FormatID == imageFormat.Guid)
            {
                return imageCodecInfo;
            }
        }
        return null;
    }
    View Code

    代码:

    MemoryStream ms = HttpUtil.HttpDownloadFile(url);
    Bitmap bmp = new Bitmap(ms);
    
    EncoderParameters encoderParameters = new EncoderParameters(1);
    EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
    encoderParameters.Param[0] = encoderParameter;
    
    MemoryStream msCompress = new MemoryStream();
    bmp.Save(msCompress, GetImageCodecInfo(ImageFormat.Jpeg), encoderParameters);
    Bitmap bmpCompress = new Bitmap(msCompress);
    bmpCompress.Save(path);
    bmp.Save(path2);
    
    msCompress.Close();
    ms.Close();
    View Code

     代码:

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SunCreate.Common.ComLib
    {
        /// <summary>
        /// 图片缩放
        /// </summary>
        public class ZoomImageUtil
        {
            #region 图片缩放
            /// <summary>
            /// 图片缩放
            /// </summary>
            /// <param name="bArr">图片字节流</param>
            /// <param name="width">目标宽度,若为0,表示宽度按比例缩放</param>
            /// <param name="height">目标长度,若为0,表示长度按比例缩放</param>
            public static byte[] GetThumbnail(byte[] bArr, int width, int height)
            {
                if (bArr == null) return null;
                MemoryStream ms = new MemoryStream(bArr);
                Bitmap bmp = (Bitmap)Image.FromStream(ms);
                ms.Close();
    
                bmp = GetThumbnail(bmp, width, height);
    
                ImageCodecInfo imageCodecInfo = GetEncoder(ImageFormat.Jpeg);
                EncoderParameters encoderParameters = new EncoderParameters(1);
                EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
                encoderParameters.Param[0] = encoderParameter;
    
                ms = new MemoryStream();
                bmp.Save(ms, imageCodecInfo, encoderParameters);
                byte[] result = ms.ToArray();
                ms.Close();
                bmp.Dispose();
    
                return result;
            }
            #endregion
    
            #region 图片缩放
            /// <summary>
            /// 图片缩放
            /// </summary>
            /// <param name="bmp">图片</param>
            /// <param name="width">目标宽度,若为0,表示宽度按比例缩放</param>
            /// <param name="height">目标长度,若为0,表示长度按比例缩放</param>
            private static Bitmap GetThumbnail(Bitmap bmp, int width, int height)
            {
                if (width == 0 && height == 0)
                {
                    width = bmp.Width;
                    height = bmp.Height;
                }
                else
                {
                    if (width == 0)
                    {
                        width = height * bmp.Width / bmp.Height;
                    }
                    if (height == 0)
                    {
                        height = width * bmp.Height / bmp.Width;
                    }
                }
    
                Image imgSource = bmp;
                Bitmap outBmp = new Bitmap(width, height);
                Graphics g = Graphics.FromImage(outBmp);
                g.Clear(Color.Transparent);
                // 设置画布的描绘质量     
                g.CompositingQuality = CompositingQuality.Default;
                g.SmoothingMode = SmoothingMode.Default;
                g.InterpolationMode = InterpolationMode.Default;
                g.DrawImage(imgSource, new Rectangle(0, 0, width, height + 1), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
                g.Dispose();
                imgSource.Dispose();
                bmp.Dispose();
                return outBmp;
            }
            #endregion
    
            #region 椭圆形缩放
            /// <summary>
            /// 椭圆形缩放
            /// </summary>
            /// <param name="bArr">图片字节流</param>
            /// <param name="width">目标宽度,若为0,表示宽度按比例缩放</param>
            /// <param name="height">目标长度,若为0,表示长度按比例缩放</param>
            public static byte[] GetEllipseThumbnail(byte[] bArr, int width, int height)
            {
                if (bArr == null) return null;
                MemoryStream ms = new MemoryStream(bArr);
                Bitmap bmp = (Bitmap)Image.FromStream(ms);
    
                Bitmap newBmp = new Bitmap(width, height);
                using (Graphics g = Graphics.FromImage(newBmp))
                {
                    using (TextureBrush br = new TextureBrush(bmp))
                    {
                        br.ScaleTransform(width / (float)bmp.Width, height / (float)bmp.Height);
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        g.FillEllipse(br, new Rectangle(Point.Empty, new Size(width, height)));
                    }
                }
                MemoryStream newMs = new MemoryStream();
                newBmp.Save(newMs, System.Drawing.Imaging.ImageFormat.Png);
                byte[] result = newMs.ToArray();
    
                bmp.Dispose();
                newBmp.Dispose();
                ms.Close();
                newMs.Dispose();
    
                return result;
            }
            #endregion
    
            #region GetEncoder
            private static ImageCodecInfo GetEncoder(ImageFormat format)
            {
                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
                foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.FormatID == format.Guid)
                    {
                        return codec;
                    }
                }
                return null;
            }
            #endregion
    
        }
    }
    View Code
  • 相关阅读:
    洛谷 P2831 [NOIP2016]愤怒的小鸟
    洛谷 P1736 创意吃鱼法
    洛谷 P2347 砝码称重 + bitset简析
    洛谷 P3384 [模板] 树链剖分
    洛谷 P1038 [NOIP2012] 借教室
    洛谷 P3959 [NOIP2017]宝藏 题解
    洛谷 AT2167 Blackout 题解
    洛谷 P1246 编码 题解
    C#中ref关键字的用法总结
    C#中的值传递与引用传递(in、out、ref)
  • 原文地址:https://www.cnblogs.com/s0611163/p/9529497.html
Copyright © 2011-2022 走看看