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

系列文章

C#底层库–记录日志帮助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/124187709

C#底层库–数据库访问帮助类(MySQL版)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126886379

C#底层库–获取文件版本和MD5值
本文链接:https://blog.csdn.net/youcheng_ge/article/details/112513871

C#底层库–操作文件帮助类FileHelper(获取目录的所有文件)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126887161

C#底层库–操作Excel帮助类(读取、导出表格)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126887445

C#底层库–软件版本管理XML
本文链接:https://blog.csdn.net/youcheng_ge/article/details/110195766

C#底层库–随机数生成类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126888812

C#底层库–正则表达式帮助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/109745286

C#底层库–CSV和DataTable相互转换
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128804367

文章目录

  • 系列文章
  • 前言
  • 一、底层库介绍
  • 二、底层库源码
  • 三、调用方法
  • 三、项目案列


前言

本专栏为【底层库】,将介绍研发过程中 通用的函数。我们将这些固化的源码,进行重写、规范封装、单元测试、集成测试,从而形成通用化模块库,本专栏称为“底层库”。

作为研发人员的你,并不需要花大量时间,完全掌握“底层库”的含义,你只需要几行调用代码,就可以解决一些项目上碰到的难题。大家有任何问题,可以评论区反馈,私信我。

底层库已实现功能:数据库操作、加解密算法、日志记录、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
{
    /// 
    /// 图片处理
    /// 
    public class ImgHelper
    {        
        /// 
        /// 生成缩略图
        /// 
        /// 源文件
        /// 缩略图
        /// 
        /// 
        /// 
        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();
            }
        }
        /// 
        /// 获取网络图片
        /// 
        /// 
        /// 
        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);
            }
        }
        /// 
        /// 将图片裁剪成圆形
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        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;
        }

        /// 
        /// 将图片裁剪成圆形
        /// 
        /// 
        /// 
        /// 
        /// 
        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;
        }
    }
}


三、调用方法

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

暂无

三、项目案列

你可能感兴趣的:(C#,底层库(工具,通用类),c#,开发语言)