C#底层库–Image图片操作类

  • Post author:
  • Post category:其他




系列文章

C#底层库–记录日志帮助类

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/124187709

C#底层库–MySQLBuilder脚本构建类(select、insert、update、in、带条件的SQL自动生成)

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/129179216

C#底层库–MySQL数据库访问操作辅助类(推荐阅读)

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/126886379

C#底层库–XML配置参数读写辅助类(推荐阅读)

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/129175304

C#底层库–获取文件版本和MD5值

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/112513871

C#底层库–FilesHelper文件辅助类(删除目录文件、复制文件到指定目录)

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/126887161

C#底层库–操作Excel帮助类(读取、导出表格)

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/126887445

C#底层库–随机数生成器

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/126888812

C#底层库–RegexHelper正则表达式辅助类

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/109745286

C#底层库–CSV和DataTable相互转换

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/128804367

C#底层库–Image图片操作类

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/128805298

C#底层库–JSON帮助类_详细(序列化、反序列化、list、datatable)

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/128805705

C#底层库–cookie操作辅助类

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/128816347

C#底层库–Session操作辅助类

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/128817096

C#底层库–Image图片操作类

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/128805298

C#底层库–数据库类型与程序类型转换类

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/128817610

C#底层库–StringExtension字符串扩展类

本文链接:

https://blog.csdn.net/youcheng_ge/article/details/129520428




前言

本专栏为【底层库】,主要介绍编程过程中 通用函数。我们将这些通用固化的源码,进行重写、封装、拓展,再进行单元测试、集成测试、beta测试,最终形成通用化模板,这里我们称为“底层库”。

作为研发人员的你,并不需要花大量时间,研究“底层库”的含义,及“底层库”的实现方法。你只需要几行调用代码,就可以解决项目上碰到的难题。而调用方法、项目样例本文均有介绍,大家有任何问题,可以私信我,如果你对本栏感兴趣关注一下。

底层库已实现功能:数据库操作、加解密算法、日志记录、HTTP通信、Socket通信、API前后端交互、邮件发送、文件操作、配置参数存储、Excel导入导出、CSV和DataTable转换、压缩解压、自动编号、Session操作等。

在这里插入图片描述



一、底层库介绍

图片处理类。支持生成缩略图、获取网络图片、将图片裁剪成圆形、将图片裁剪成圆形。



二、底层库源码

创建类ImgHelper.cs,复制以下代码。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Text;

namespace Geyc_Utils.Commons.Helpers
{
    /// <summary>
    /// 图片处理
    /// </summary>
    public class ImgHelper
    {        
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalImagePath">源文件</param>
        /// <param name="thumbnailPath">缩略图</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        /// <param name="mode"></param>
        public static void MakeThumbnail(string originalImagePath,
            string thumbnailPath,
            int width = 120, int height = 90, string mode = "H")
        {
            Image image = Image.FromFile(originalImagePath);
            if (image.Width <= width && image.Height <= height)
            {
                File.Copy(originalImagePath, thumbnailPath, true);
                image.Dispose();
            }
            else
            {
                int width2 = image.Width;
                int height2 = image.Height;
                float num = (float)height / (float)height2;
                if ((float)width / (float)width2 < num)
                {
                    num = (float)width / (float)width2;
                }
                width = (int)((float)width2 * num);
                height = (int)((float)height2 * num);
                Image image2 = new Bitmap(width, height);
                Graphics graphics = Graphics.FromImage(image2);
                graphics.Clear(Color.White);
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width2, height2), GraphicsUnit.Pixel);
                EncoderParameters encoderParameters = new EncoderParameters();
                EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                encoderParameters.Param[0] = encoderParameter;
                ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo encoder = null;
                int num2 = 0;
                while (num2 < imageEncoders.Length)
                {
                    if (!imageEncoders[num2].FormatDescription.Equals("JPEG"))
                    {
                        num2++;
                        continue;
                    }
                    encoder = imageEncoders[num2];
                    break;
                }
                image2.Save(thumbnailPath, encoder, encoderParameters);
                encoderParameters.Dispose();
                encoderParameter.Dispose();
                image.Dispose();
                image2.Dispose();
                graphics.Dispose();
            }
        }
        /// <summary>
        /// 获取网络图片
        /// </summary>
        /// <param name="imgUrl"></param>
        /// <returns></returns>
        public static Bitmap GetNetImg(string imgUrl)
        {
            try
            {
                Random random = new Random();
                imgUrl = ((!imgUrl.Contains("?")) ? (imgUrl + "?aid=" + random.NextDouble()) : (imgUrl + "&aid=" + random.NextDouble()));
                WebRequest webRequest = WebRequest.Create(imgUrl);
                WebResponse response = webRequest.GetResponse();
                Stream responseStream = response.GetResponseStream();
                Image image = Image.FromStream(responseStream);
                responseStream.Close();
                responseStream.Dispose();
                webRequest = null;
                response = null;
                return (Bitmap)image;
            }
            catch (Exception ex)
            {
                Log4NetHelper.Error("获取网络图片错误", ex); 
                return new Bitmap(100, 100);
            }
        }
        /// <summary>
        /// 将图片裁剪成圆形
        /// </summary>
        /// <param name="img"></param>
        /// <param name="rec"></param>
        /// <param name="size"></param>
        /// <param name="imgSavePath"></param>
        /// <returns></returns>
        public static Image CutEllipse(Image img, Rectangle rec, Size size, string imgSavePath)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
                {
                    br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.FillEllipse(br, new Rectangle(Point.Empty, size));
                }
            }
            bitmap.Save(imgSavePath, System.Drawing.Imaging.ImageFormat.Png);
            return bitmap;
        }

        /// <summary>
        /// 将图片裁剪成圆形
        /// </summary>
        /// <param name="img"></param>
        /// <param name="rec"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static Bitmap CutEllipse(Image img, Rectangle rec, Size size)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
                {
                    br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.FillEllipse(br, new Rectangle(Point.Empty, size));
                }
            }
            return bitmap;
        }
    }
}




三、调用方法

我放在了窗体页面,新增一个按钮,点击按钮时触发。

暂无



三、项目案列



版权声明:本文为youcheng_ge原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。