/// <summary>
        /// 
        /// </summary>
        /// <param name="sFile">原图片</param>
        /// <param name="dFile">压缩后保存位置</param>
        /// <param name="dHeight">高度</param>
        /// <param name="dWidth">宽度</param>
        /// <param name="flag">压缩质量 1-100</param>
        public static bool GetPicThumbnail(string sFile, string dFile, int flag)
        {
            Bitmap original = (Bitmap)Image.FromFile(sFile);
            ImageFormat tFormat = original.RawFormat;
            //获取图片宽度
            int sourceWidth = original.Width;
            //获取图片高度
            int sourceHeight = original.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;
            //计算宽度的缩放比例
            nPercentW = ((float)300 / (float)sourceWidth);
            //计算高度的缩放比例
            nPercentH = ((float)3000 / (float)sourceHeight);
            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;
            //期望的宽度
            int destWidth = (int)(sourceWidth * nPercent);
            //期望的高度
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmp = new Bitmap(original, new Size(destWidth, destHeight*2));
            //bmp.SetResolution(original.HorizontalResolution, original.VerticalResolution);
            using (var g = Graphics.FromImage(bmp))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(original, 0, 0, destWidth, destHeight*2);
                //g.Clear(Color.White);
                //g.DrawImageUnscaled(original, 0, 0);
            }
            //保存图片时,压缩图片
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;//设置压缩的比例1-10
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;
            try
            {
                //图片背景透明
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x++)
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                if (jpegICIinfo != null)
                {
                    bmp.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
                }
                else
                {
                    bmp.Save(dFile, tFormat);
                }
                return true;
            }
            catch(Exception ex)
            {

                return false;
            }
            finally
            {
                original.Dispose();
                bmp.Dispose();
            }
        }

 

posted on 2022-04-24 18:07  水。  阅读(217)  评论(0编辑  收藏  举报