C# 图片压缩

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
 
namespace ImgCompress
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "1.png";                  // debug->bin 下必须有 1.png 这个文件
            string destFile = "compress\\1.png";    // debug->bin 下必须有 compress 这个文件夹
 
            Stopwatch sw = Stopwatch.StartNew();
            Bitmap srcBitMap = (Bitmap)Bitmap.FromFile(path, false);
            Compress(srcBitMap, destFile, 50);
            Console.WriteLine(sw.ElapsedMilliseconds);
 
            Console.Read();
        }
 
        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

 /// <summary>
        /// 图片压缩(降低质量以减小文件的大小)
        /// </summary>
        /// <param name="srcBitmap">传入的Bitmap对象</param>
        /// <param name="destStream">压缩后的Stream对象</param>
        /// <param name="level">压缩等级,0到100,0 最差质量,100 最佳</param>
        public static void Compress(Bitmap srcBitmap, Stream destStream, long level)
        {
            ImageCodecInfo myImageCodecInfo;
            System.Drawing.Imaging.Encoder myEncoder;
            EncoderParameter myEncoderParameter;
            EncoderParameters myEncoderParameters;
 
            // Get an ImageCodecInfo object that represents the JPEG codec.
            myImageCodecInfo = GetEncoderInfo("image/jpeg");
 
            // Create an Encoder object based on the GUID
 
            // for the Quality parameter category.
            myEncoder = System.Drawing.Imaging.Encoder.Quality;
 
            // Create an EncoderParameters object.
            // An EncoderParameters object has an array of EncoderParameter
            // objects. In this case, there is only one
 
            // EncoderParameter object in the array.
            myEncoderParameters = new EncoderParameters(1);
 
            // Save the bitmap as a JPEG file with 给定的 quality level
            myEncoderParameter = new EncoderParameter(myEncoder, level);
            myEncoderParameters.Param[0] = myEncoderParameter;
            srcBitmap.Save(destStream, myImageCodecInfo, myEncoderParameters);
        }

  /// <summary>
        /// 图片压缩(降低质量以减小文件的大小)
        /// </summary>
        /// <param name="srcBitMap">传入的Bitmap对象</param>
        /// <param name="destFile">压缩后的图片保存路径</param>
        /// <param name="level">压缩等级,0到100,0 最差质量,100 最佳</param>
        public static void Compress(Bitmap srcBitMap, string destFile, long level)
        {
            Stream s = new FileStream(destFile, FileMode.Create);
            Compress(srcBitMap, s, level);
            s.Close();
        }

       //确定路径
                    string filePath = "/wx_images/" + Datahelp.GetCurrentTimeString() + "1" + "."+ suffix;
                    string physicalPath = HttpContext.Current.Server.MapPath("~" + filePath);
 
                    //如果 > 1MB, 则先压缩
                    if (bytes > 1024 * 1024 * 1)
                    {
                        Stream stream = _upfile.InputStream;
                        byte[] byteArr = new byte[stream.Length];
                        stream.Read(byteArr, 0, byteArr.Length);
                        stream.Seek(0, SeekOrigin.Begin);       //设置当前流的位置为流的开始
 
                        //将 byteArr 转为Bitmap
                        Bitmap srcBitMap = new Bitmap((Image)new Bitmap(stream));
                        ImageHelper.Compress(srcBitMap, physicalPath, 50);
                    }
                    else
                    {
                        _upfile.SaveAs( physicalPath );//保存图片  
                    }
                    tb.Add("state", 1);

                    tb.Add("filePath", "http://" + HttpContext.Current.Request.Url.Host.ToString() + ":" +                                                                        HttpContext.Current.Request.Url.Port + filePath);

                    原文:https://blog.csdn.net/yenange/article/details/89306473

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值