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

            /// <summary>

            /// 图像缩略图处理

            /// </summary>

            /// <param name="bytes">图像源数据</param>

            /// <param name="compression">压缩质量 1-100</param>

            /// <param name="thumbWidth">缩略图的宽</param>

            /// <param name="thumbHeight">缩略图的高</param>

            /// <returns></returns>

            public static byte[] ConvertToThumbnail(byte[] bytes, int compression = 100, int thumbWidth = 0, int thumbHeight = 0)

            {

                byte[] bs = null;


                try

                {

                    if (bytes != null)

                    {

                        using (MemoryStream ms = new MemoryStream(bytes))

                        {

                            using (Bitmap srcimg = new Bitmap(ms))

                            {

                                if (thumbWidth == 0 && thumbHeight == 0)

                                {

                                    thumbWidth = srcimg.Width;

                                    thumbHeight = srcimg.Height;

                                }

                                using (Bitmap dstimg = new Bitmap(thumbWidth, thumbHeight))//图片压缩质量

                                {

                                    //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。

                                    using (Graphics gr = Graphics.FromImage(dstimg))

                                    {

                                        //把原始图像绘制成上面所设置宽高的缩小图

                                        Rectangle rectDestination = new Rectangle(0, 0, thumbWidth, thumbHeight);

                                        gr.Clear(Color.WhiteSmoke);

                                        gr.CompositingQuality = CompositingQuality.HighQuality;

                                        gr.SmoothingMode = SmoothingMode.HighQuality;

                                        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;

                                        gr.DrawImage(srcimg, rectDestination, 0, 0, srcimg.Width, srcimg.Height, GraphicsUnit.Pixel);


                                        EncoderParameters ep = new EncoderParameters(1);

                                        ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression);//设置压缩的比例1-100

                                        ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();

                                        ImageCodecInfo jpegICIinfo = arrayICI.FirstOrDefault(t => t.FormatID == System.Drawing.Imaging.ImageFormat.Png.Guid);

                                        using (MemoryStream dstms = new MemoryStream())

                                        {

                                            if (jpegICIinfo != null)

                                            {

                                                dstimg.Save(dstms, jpegICIinfo, ep);

                                            }

                                            else

                                            {

                                                dstimg.Save(dstms, System.Drawing.Imaging.ImageFormat.Png);//保存到内存里

                                            }

                                            bs = new Byte[dstms.Length];

                                            dstms.Position = 0;

                                            dstms.Read(bs, 0, bs.Length);

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

                catch (Exception ex)

                {

                    LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("ConvertToThumbnail error.", thumbWidth, " ", thumbHeight));

                }

                return bs;

            }


  • 相关阅读:
    mysql数据库常用指令
    解决windows的mysql无法启动 服务没有报告任何错误的经验。
    “Can't open file for writing”或“operation not permitted”的解决办法
    启动Apache出现错误Port 80 in use by "Unable to open process" with PID 4!
    如何打开windows的服务services.msc
    常见的HTTP状态码 404 500 301 200
    linux系统常用的重启、关机指令
    (wifi)wifi移植之命令行调试driver和supplicant
    linux(debian)安装USB无线网卡(tp-link TL-WN725N rtl8188eu )
    alloc_chrdev_region申请一个动态主设备号,并申请一系列次设备号
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12352538.html
Copyright © 2011-2022 走看看