.netCore 图片压缩处理

.netCore 图片压缩处理

参考了一些开源的代码,自己加工了一下,相当好用的图片压缩实现。 代码简单,返回的是byte[], 当然也可以直接保存成文件。

图片压缩处理



using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

namespace SuperErp.Common
{
    public class ImageParam
    {
        public int Width { get; set; }
        public int Height { get; set; }

        public int ToWidth { get; set; }
        public int ToHeight { get; set; } 
        public string Mode { get; set; }
    }

    public class ImageHelper
    {
        public static byte[] CompressImage(IFormFile file, int toWidth, int toHeight, string mode = "HW")
        { 
            var compressImageBytes = new byte[] { }; 

            //上传文件的byte数组转为Stream 
            Image img = ReadStreamToImage(file);

            //获得图片宽高比例 
             

            ImageParam imageParam = new ImageParam() { 
                Height = img.Height,
                Width = img.Width,
                ToHeight = toHeight,
                ToWidth = toWidth,
                Mode = mode
            };
            //可以按MODE调整宽高
            InitImageParameters(imageParam);

            //按照新的宽高用画布重新画一张
            Bitmap bitmap = DrawImage(img, imageParam);  
            return GetBitmapBytes(bitmap);
        }

        private static Image ReadStreamToImage(IFormFile file)
        { 
            var length = (int)file.Length;
            var stream = file.OpenReadStream();
            var imageBytes = new byte[length];
            stream.Read(imageBytes, 0, length); 
            return Image.FromStream(stream);
        }

        private static void InitImageParameters(ImageParam imageParam)
        { 
            switch (imageParam.Mode)
            {
                case "HW"://指定高宽缩放(可能变形)                
                    break;
                case "W"://指定宽,高按比例                    
                    imageParam.ToHeight = imageParam.Height * imageParam.ToWidth / imageParam.Width; 
                    break;
                case "H"://指定高,宽按比例
                    imageParam.ToWidth = imageParam.Width * imageParam.ToHeight / imageParam.Height; 
                    break;
                default:
                    break;
            } 
        }

        private static Bitmap DrawImage(Image img,ImageParam imageParam) {

            Bitmap bitmap = new Bitmap(imageParam.ToWidth, imageParam.ToHeight);
            Graphics g = Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            g.DrawImage(img, new Rectangle(0, 0, imageParam.ToWidth, imageParam.ToHeight), new Rectangle(0, 0, imageParam.Width, imageParam.Height), GraphicsUnit.Pixel);
            return bitmap;
        }

        private static byte[] GetBitmapBytes(Bitmap bitmap)
        {
            var compressImageBytes = new byte[] { };
            using (MemoryStream memoryStream = new MemoryStream())
            {
                bitmap.Save(memoryStream, ImageFormat.Png);
                compressImageBytes = new byte[memoryStream.Length];
                memoryStream.Seek(0, SeekOrigin.Begin);
                memoryStream.Read(compressImageBytes, 0, Convert.ToInt32(memoryStream.Length));
            }
            return compressImageBytes;
        }
    } 
}



 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值