.net 如何压缩图片并上传

BillHu233 2019-06-12 09:18:05


如图, 通过file input 上传了一个文件, 如何压缩并上传

不要来个 xxx.Save()啥的, 这边不支持直接保存的,是上传到文件服务器, 所以要直接压缩了转换成steam上传上去
...全文
797 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghui0380 2019-06-17
  • 打赏
  • 举报
回复
??我想说http本来就支持gzip https://blog.csdn.net/cyydqc/article/details/44464353
闭包客 2019-06-17
  • 打赏
  • 举报
回复
https://gitee.com/bibaoke/Less.Image 我写的图片裁剪项目。
qq_39508204 2019-06-14
  • 打赏
  • 举报
回复
将上传的图片 转为image 格式 传入第一个参数 生成的图片地址 传入第二个参数 设置压缩比(1到100) 传入第三个参数 实现上传前压缩 质量基本无损

      public static bool CompressImage(System.Drawing.Image sFile, string dFile, int flag = 90)
        {
            
            System.Drawing.Image iSource = sFile;
            ImageFormat tFormat = iSource.RawFormat;
            int dHeight = iSource.Height / 2;
            int dWidth = iSource.Width / 2;
            int sW = 0, sH = 0;
            //按比例缩放
            Size tem_size = new Size(iSource.Width, iSource.Height);
            if (tem_size.Width > dHeight || tem_size.Width > dWidth)
            {
                if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
                {
                    sW = dWidth;
                    sH = (dWidth * tem_size.Height) / tem_size.Width;
                }
                else
                {
                    sH = dHeight;
                    sW = (tem_size.Width * dHeight) / tem_size.Height;
                }
            }
            else
            {
                sW = tem_size.Width;
                sH = tem_size.Height;
            }

            Bitmap ob = new Bitmap(dWidth, dHeight);
            Graphics g = Graphics.FromImage(ob);

            g.Clear(Color.WhiteSmoke);
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);

            g.Dispose();

            //以下代码为保存图片时,设置压缩质量
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;//设置压缩的比例1-100
            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)
                {
                    ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
                    FileInfo fi = new FileInfo(dFile);
                
                }
                else
                {
                    ob.Save(dFile, tFormat);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                iSource.Dispose();
                ob.Dispose();
            }
        }
BillHu233 2019-06-13
  • 打赏
  • 举报
回复
网上查了很多都写的太复杂了, 这里自己整理了一份好像可行

if (System.Web.HttpContext.Current.Request.Files.Count == 0)
{
throw new Exception("没有选择文件!");
}

//这里接收来自前台上传的图片
var file = System.Web.HttpContext.Current.Request.Files[0];

//文件将要压缩的比例(file.ContentLength是当前文件的大小,这里默认将文件压缩为1024kb,可以自己调)
double compressionRatio = 1024 * 1024 / Convert.ToDouble(file.ContentLength);
compressionRatio = Math.Round(compressionRatio, 2);

//上传文件转为byte数组
byte[] fileByte = new byte[file.ContentLength];
file.InputStream.Read(fileByte, 0, file.ContentLength);

//上传文件的byte数组转为Stream
MemoryStream ms = new MemoryStream(fileByte);
Image img = Image.FromStream(ms);

//按比例计算新的宽高
int toWidth = Convert.ToInt32(img.Width * compressionRatio);
int toHeight = Convert.ToInt32(img.Height * compressionRatio);

//按照新的宽高用画布重新画一张
Bitmap bitmap = new Bitmap(toWidth, toHeight);
Graphics g = 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(img, new System.Drawing.Rectangle(0, 0, toWidth, toHeight), new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);

//将画好的bitmap转成stream(不一定费时stream,byte数组什么都可以)
var fileStream = new MemoryStream();

using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Png);
byte[] data = new byte[stream.Length];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(data, 0, Convert.ToInt32(stream.Length));
fileStream = new MemoryStream(data);
}

//至此,拿到压缩好的stream或者bytes或者什么什么的 不管是上传还是前台显示都可以了(#^.^#)
outby 2019-06-12
  • 打赏
  • 举报
回复
你要压缩后上传,那就要用到JS了,用C#都是先上传再压缩的。
  • 打赏
  • 举报
回复
https://blog.csdn.net/starfd/article/details/54575199
最底下有压缩代码,然后不管是保存到本地,还是到Stream,都是要调用Image的Save方法的

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧