posts - 21,comments - 64,views - 25015

    /// <summary>
    /// 图片处理工具类
    /// </summary>
    public class ImageUtils
    {
        /// <summary>
        /// 产生缩略图
        /// </summary>
        public static System.IO.Stream GenerateThumbnail(System.IO.Stream imageStream, int width, int height)
        {
            System.IO.Stream ms;

            using (var originalImage = System.Drawing.Image.FromStream(imageStream))
            {
                int towidth = width;
                int toheight = height;

                int x = 0;
                int y = 0;
                int ow = originalImage.Width;
                int oh = originalImage.Height;


                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y = 0;
                    x = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * height / towidth;
                    x = 0;
                    y = (originalImage.Height - oh) / 2;
                }

                using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight))
                using (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(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                    new System.Drawing.Rectangle(x, y, ow, oh),
                    System.Drawing.GraphicsUnit.Pixel);

                    ms = new System.IO.MemoryStream();
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }

            return ms;
        }

        /// <summary>
        /// 产生缩略图
        /// </summary>
        public static void GenerateThumbnail(System.IO.Stream imageStream, int width, int height, string outputFilePath)
        {
            using (var stream = GenerateThumbnail(imageStream, width, height))
            using (var fileStream = System.IO.File.Open(outputFilePath, System.IO.FileMode.OpenOrCreate))
            {
                ((System.IO.MemoryStream)stream).WriteTo(fileStream);
            }
        }

        /// <summary>
        /// 产生缩略图
        /// </summary>
        public static System.IO.Stream GenerateThumbnail(byte[] imageBytes, int width, int height)
        {
            System.IO.Stream ms;

            using (var stream = new System.IO.MemoryStream(imageBytes))
            {
                ms = GenerateThumbnail(stream, width, height);
            }

            return ms;
        }

        /// <summary>
        /// 产生缩略图
        /// </summary>
        public static void GenerateThumbnail(byte[] imageBytes, int width, int height, string outputFilePath)
        {
            using (var stream = GenerateThumbnail(imageBytes, width, height))
            using (var fileStream = System.IO.File.Open(outputFilePath, System.IO.FileMode.OpenOrCreate))
            {
                ((System.IO.MemoryStream)stream).WriteTo(fileStream);
            }
        }

        /// <summary>
        /// 产生缩略图
        /// </summary>
        public static System.IO.Stream GenerateThumbnail(string imageFilePath, int width, int height)
        {
            System.IO.Stream ms;

            using (var stream = new System.IO.FileStream(imageFilePath, System.IO.FileMode.Open))
            {
                ms = GenerateThumbnail(stream, width, height);
            }

            return ms;
        }

        /// <summary>
        /// 产生缩略图
        /// </summary>
        public static void GenerateThumbnail(string imageFilePath, int width, int height, string outputFilePath)
        {
            using (var stream = GenerateThumbnail(imageFilePath, width, height))
            using (var fileStream = System.IO.File.Open(outputFilePath, System.IO.FileMode.OpenOrCreate))
            {
                ((System.IO.MemoryStream)stream).WriteTo(fileStream);
            }
        }

posted on   王庭安  阅读(438)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
< 2011年7月 >
26 27 28 29 30 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6

点击右上角即可分享
微信分享提示