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

系列文章

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;
        }
    }
}


三、调用方法

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

暂无

三、项目案列

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: el-image 是一个 Vue.js 的组件,用于在网页上展示图片。你可以通过使用 Vue.js 的 v-for 指令来遍历加载图片。 例如,假设你有一个图片数组,包含了你想要加载的图片的 URL,你可以这样使用 el-image 组件来遍历加载这些图片: ``` <template> <div> <el-image v-for="(image, index) in images" :key="index" :src="image"></el-image> </div> </template> <script> export default { data() { return { images: [ 'https://example.com/image1.jpg', 'https://example.com/image2.jpg', 'https://example.com/image3.jpg' ] } } } </script> ``` 在上面的代码中,我们使用了 v-for 指令来遍历 images 数组,并且使用 el-image 组件来展示图片。我们还使用了 :key 属性来提供每个 el-image 组件的唯一标识符,这是为了优化 Vue.js 的性能。:src 属性用于指定图片的 URL。 希望这个示例能帮助你了解如何使用 el-image 组件来遍历加载图片。 ### 回答2: el-image是Element UI框架中的一个组件,用于加载图片。要实现el-image的遍历加载图片,可以按照以下步骤进行操作。 首先,需要准备一组图片的数据,可以使用一个数组来存储图片的URL地址或者其他相关信息。 然后,在Vue组件中使用v-for指令遍历这个图片数组。可以在一个容器元素内使用v-for指令,例如使用div元素作为容器,并在该容器内部使用el-image组件。 接下来,将图片数组作为v-for指令的参数,即可在每次循环中获取到数组中的一项图片数据。在el-image组件中使用:src属性绑定该图片数据的URL地址,这样就能够加载和显示该图片。 最后,可以根据需要设置el-image组件的其他属性,例如设置图片的宽高、裁剪模式等。 总结一下,使用el-image组件进行遍历加载图片的步骤是:准备图片数据数组,使用v-for指令遍历数组,在组件内部使用el-image组件加载每个图片数据,设置图片的基本属性。 ### 回答3: el-image是Element UI中的一种图片组件,用于显示图片。如果要遍历加载多张图片,可以利用v-for指令和循环遍历的方式来动态生成el-image组件。 首先,在data中定义一个图片数组,包含了要加载的所有图片的路径。例如: ``` data() { return { imageList: [ { url: 'image1.jpg' }, { url: 'image2.jpg' }, { url: 'image3.jpg' } ] } } ``` 然后,在模板中使用v-for指令来遍历imageList数组,生成对应的el-image组件。例如: ``` <template> <div> <el-image v-for="image in imageList" :key="image.url" :src="image.url"> </el-image> </div> </template> ``` 这样就可以通过遍历加载图片,生成多个el-image组件,并根据imageList数组中的图片路径来设置el-image的src属性,以显示对应的图片。 需要注意的是,为了保证图片能正常加载,需要确保图片路径是正确的,并且可以访问到。另外,如果图片过多或者较大,可能会导致页面加载速度变慢,因此在实际使用过程中,应该合理控制加载图片的数量和大小,以提升页面性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花北城

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值