图片等比例压缩方法

本文探讨了如何实现图片的等比例压缩,包括使用string、byte数组和stream操作照片文件的方法,同时避免出现null值问题,确保高效且不失真的图片压缩解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GetPic(picstream, ref RealWidth, ref RealHeight, ref t);

FileStream fs = File.Create(filepath + "\\Photos\\" + filename);
                    fs.Write(t, 0, t.Length);
                    fs.Flush();
                    fs.Close();
                    fs.Dispose();

GetReduceImage(filepath + "\\Photos\\" + filename, filepath + "\\Photos\\", sltName, "0");


 

/// <summary>
        /// 获取照片的实际宽度,实际高度等
        /// </summary>
        /// <param name="picstream">照片流</param>
        /// <param name="RealWidth">实际宽度</param>
        /// <param name="RealHeight">实际高度</param>
        /// <param name="t"></param>
        public void GetPic(Stream picstream, ref int RealWidth, ref int RealHeight, ref byte[] t)
        {
            t = new byte[picstream.Length];
            picstream.Read(t, 0, (int)picstream.Length);

            Image image = new Bitmap(picstream);
            picstream.Flush();
            picstream.Close();
            picstream.Dispose();

            RealWidth = image.Width;
            RealHeight = image.Height;

            if (image != null)
            {
                image.Dispose();
                image = null;
            }
        }


 

/// <summary>
        /// 生成压缩图
        /// </summary>
        /// <param name="objectPath">源文件路径</param>
        /// <param name="targetFilePath">压缩文件路径</param>
        /// <param name="tName">压缩文件名称</param>
        public void GetReduceImage(string objectPath, string targetFilePath, string tName, string flag)
        {

            Image ResourceImage = Image.FromFile(objectPath);

            GetThumbWandH(ResourceImage.Width, ResourceImage.Height, flag);  //获取压缩的宽和高

            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(ThumbW, ThumbH);

            //新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(ResourceImage, new System.Drawing.Rectangle(0, 0, ThumbW, ThumbH),
                new System.Drawing.Rectangle(0, 0, ResourceImage.Width, ResourceImage.Height),
                System.Drawing.GraphicsUnit.Pixel);

            try
            {
                bitmap.Save(@targetFilePath + tName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                ResourceImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }

        public void GetThumbWandH(int Fwith, int Fheight, string bs)
        {
            if (bs == "1")
            {
                ThumbW = 50;
                ThumbH = 50;
                return;
            }

            if (Fwith >= Fheight && Fwith > StandW)
            {
                ThumbW = StandW;
                ThumbH = StandW * Fheight / Fwith;
            }
            else if ((Fwith > Fheight && Fheight > StandH) || (Fwith <= Fheight && Fheight > StandH))
            {
                ThumbW = StandH * Fwith / Fheight;
                ThumbH = StandH;
            }
            else
            {
                ThumbW = Fwith;
                ThumbH = Fheight;
            }

            if (ThumbW <= StandW && ThumbH <= StandH)
            {
                return;
            }
            GetThumbWandH(ThumbW, ThumbH, bs);
        }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值