在进行图片处理时,需要把用户上传的图片放在画布上面,由于用户上传的图片分辨率比较的大,所以我们需要对图片进行缩放已适应我们所创建的画布。图片缩放需要处理的情况比较的多,无非就是图片宽度高度和画布的宽度高度进行比较从而使用合适的图片缩放的分辨率大小。

        /// <summary>
        /// 等比例缩放图片
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="destHeight"></param>
        /// <param name="destWidth"></param>
        /// <returns></returns>
        public static Image ZoomImage(Image bitmap, int destHeight, int destWidth)
        {
            try
            {
                var sourImage = bitmap;
                int width = 0, height = 0;

                if (sourImage.Width > destWidth && sourImage.Height > destHeight)
                {
                    var w1 = sourImage.Width / (decimal)destWidth;
                    var h1 = sourImage.Height / (decimal) destHeight;
                    if (w1 > h1)
                    {
                        width = destWidth;
                        height = (int)(sourImage.Height / w1);
                    }
                    else if (h1 > w1)
                    {
                        width = (int)(sourImage.Width / h1);
                        height = destHeight;
                    }
                    else
                    {
                        width = sourImage.Width;
                        height = sourImage.Height;
                    }
                }
                else if (sourImage.Width > destWidth && sourImage.Height <= destHeight)
                {
                    width = sourImage.Width * (destHeight / sourImage.Height);
                    height = destHeight;
                }
                else if (sourImage.Width <= destWidth && sourImage.Height > destHeight)
                {
                    width = destWidth;
                    height = sourImage.Height * (destWidth / sourImage.Width);
                }
                else
                {
                    width = sourImage.Width;
                    height = sourImage.Height;
                }
                var destBitmap = new Bitmap(width, height);
                var g = Graphics.FromImage(destBitmap);
                g.Clear(Color.Transparent);
                //设置画布的描绘质量
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(sourImage, new Rectangle(0, 0, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
                g.Dispose();
                //设置压缩质量
                var encoderParams = new EncoderParameters();
                var quality = new long[1];
                quality[0] = 100;
                var encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[0] = encoderParam;
                sourImage.Dispose();
                return destBitmap;
            }
            catch
            {
                return bitmap;
            }
        }
最后修改:2022 年 06 月 17 日 10 : 21 AM
如果觉得我的文章对你有用,请随意赞赏