.net5生成图片缩略图,有旋转的缩略图生成回正图片

2024-06-10 09:58

本文主要是介绍.net5生成图片缩略图,有旋转的缩略图生成回正图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需要在nuget安装包System.Drawing.Common

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WebNetCore5_Img_Storage.Model.Tool
{/// <summary>/// 生成图片缩略图/// </summary>/// <remarks>/// 创建时间:2021-2-5 16:32:19/// </remarks>public static class ImageHandle{/// <summary>/// (推荐)生成缩略图,图片流输入,输出图片流/// </summary>/// <param name="dWidth">要生成的宽度</param>/// <param name="dHeight">要生成的高度</param>/// <param name="flag">生成图片质量,1-100</param>/// <param name="inputStream">输入图片流</param>/// <param name="outStream">输出图片流</param>public static void CompressImgByte(int dWidth, int dHeight, Stream inputStream, Stream outStream, int flag = 80, string arg = null){inputStream.Position = 0;System.Drawing.Image iSource = System.Drawing.Image.FromStream(inputStream);var prop = iSource.PropertyItems.FirstOrDefault(x => x.Id == 274);if (prop != null){//读取旋转方向byte[] buffed = prop.Value;StringBuilder sbv = new StringBuilder();foreach (var byteValue in buffed){sbv.Append(byteValue.ToString("x2"));}             string value2 = sbv.ToString();//Console.WriteLine($"图片id  {arg}  图片274里面的值>" + value2);//linux测试//windows获取到旋转的值为0600,linux旋转值为0006if (value2.Equals("0006") || value2.Equals("0600")){//未做任何操作,此缩略图会自动逆时针旋转90度//下面操作纠正旋转,让其顺时针旋转90度,让图片回正iSource.RotateFlip(RotateFlipType.Rotate90FlipNone);}}System.Drawing.Imaging.ImageFormat tFormat = iSource.RawFormat;//按比例缩放            if (dWidth > 0 && iSource.Width > dWidth && iSource.Width > iSource.Height){dHeight = dWidth * iSource.Height / iSource.Width;}else if (dWidth > 0 && dHeight == 0 && iSource.Width > dWidth){dHeight = dWidth * iSource.Height / iSource.Width;}else if (dWidth == 0 && dHeight > 0 && iSource.Width > iSource.Height){dWidth = dHeight * iSource.Width / iSource.Height;}else if (dHeight > iSource.Height || dWidth > iSource.Width){dWidth = iSource.Width;dHeight = iSource.Height;}else if (dHeight > 0 && iSource.Width < iSource.Height){dWidth = dHeight * iSource.Width / iSource.Height;}else{dWidth = iSource.Width;dHeight = iSource.Height;}Bitmap ob = new Bitmap(dWidth, dHeight);//ob.SetResolution(72,72);Graphics g = Graphics.FromImage(ob);//清空画布并以透明背景色填充g.Clear(System.Drawing.Color.Transparent);g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;g.DrawImage(iSource, new Rectangle(0, 0, dWidth, dHeight), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);g.Dispose();//以下代码为保存图片时,设置压缩质量  System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters();long[] qy = new long[1];qy[0] = flag;//设置压缩的比例1-100  System.Drawing.Imaging.EncoderParameter eParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);ep.Param[0] = eParam;try{System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();System.Drawing.Imaging.ImageCodecInfo jpegICIinfo = arrayICI.FirstOrDefault(x => x.FormatDescription.Equals("JPEG"));if (jpegICIinfo != null){//ob.Save("d://" + DateTime.Now.Ticks + ".jpg", jpegICIinfo, ep);ob.Save(outStream, jpegICIinfo, ep);//dFile是压缩后的新路径}else{ob.Save(outStream, tFormat);}}catch (Exception ex){string msg = ex.Message;Console.WriteLine(msg);}finally{iSource.Dispose();ob.Dispose();}}/// <summary>/// (推荐)生成缩略图,图片流输入,输出图片流/// </summary>/// <param name="dWidth">要生成的宽度</param>/// <param name="dHeight">要生成的高度</param>/// <param name="filePath">输入文件路径</param>/// <param name="outFilePath">输出保存文件路径</param>/// <param name="flag">生成的图片质量,0-100,默认80</param>public static void CompressImgFile(int dWidth, int dHeight, string filePath, string outFilePath, int flag = 80){System.Drawing.Image iSource = System.Drawing.Image.FromFile(filePath);//检测图片是否有旋转foreach (var item in iSource.PropertyItems){//System.Diagnostics.Debug.WriteLine(item.Id);if (item.Id == 274){//读取旋转方向byte[] buffed = item.Value;StringBuilder sbv = new StringBuilder();foreach (var byteValue in buffed){sbv.Append(byteValue.ToString("x2"));}//string value2 = string.Join("", buffed);string value2 = sbv.ToString();System.Diagnostics.Debug.WriteLine("方向=" + value2);//windows获取到旋转的值为0600,linux旋转值为0006if (value2.Equals("0006") || value2.Equals("0600")){//未做任何操作,此缩略图会自动逆时针旋转90度//下面操作纠正旋转,让其顺时针旋转90度,让图片回正iSource.RotateFlip(RotateFlipType.Rotate90FlipNone);}break;}}System.Drawing.Imaging.ImageFormat tFormat = iSource.RawFormat;//按比例缩放            if (dWidth > 0 && iSource.Width > dWidth && iSource.Width > iSource.Height){dHeight = dWidth * iSource.Height / iSource.Width;}else if (dWidth > 0 && dHeight == 0 && iSource.Width > dWidth){dHeight = dWidth * iSource.Height / iSource.Width;}else if (dWidth == 0 && dHeight > 0 && iSource.Width > iSource.Height){dWidth = dHeight * iSource.Width / iSource.Height;}else if (dHeight > iSource.Height || dWidth > iSource.Width){dWidth = iSource.Width;dHeight = iSource.Height;}else if (dHeight > 0 && iSource.Width < iSource.Height){dWidth = dHeight * iSource.Width / iSource.Height;}else{dWidth = iSource.Width;dHeight = iSource.Height;}Bitmap ob = new Bitmap(dWidth, dHeight);//ob.SetResolution(72,72);Graphics g = Graphics.FromImage(ob);//清空画布并以透明背景色填充g.Clear(System.Drawing.Color.Transparent);g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;g.DrawImage(iSource, new Rectangle(0, 0, dWidth, dHeight), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);g.Dispose();//以下代码为保存图片时,设置压缩质量  System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters();long[] qy = new long[1];qy[0] = flag;//设置压缩的比例1-100  System.Drawing.Imaging.EncoderParameter eParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);ep.Param[0] = eParam;try{System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();System.Drawing.Imaging.ImageCodecInfo jpegICIinfo = null;// string imgExtend = tFormat.ToString().ToUpper();string imgExtend = "JPEG";for (int x = 0; x < arrayICI.Length; x++){if (arrayICI[x].FormatDescription.Equals(imgExtend)){jpegICIinfo = arrayICI[x];break;}}if (jpegICIinfo != null){ob.Save(outFilePath, jpegICIinfo, ep);//dFile是压缩后的新路径  }else{ob.Save(outFilePath, tFormat);}}catch (Exception ex){string msg = ex.Message;Console.WriteLine(msg);}finally{iSource.Dispose();ob.Dispose();}}}
}

调用参考

输出流

 string fileInput = "E:\\测试图片\\2023_11_07_155548.3179.jpg";string outFile = "E:\\测试图片\\2023_11_07_155548.3179_c2222.jpg";//ImageHandle.CompressImgFile(1300, 1300, fileInput, outFile);var fileByter= File.ReadAllBytes(fileInput);System.IO.Stream inStream = new System.IO.MemoryStream();inStream.Write(fileByter, 0, fileByter.Length);System.IO.Stream outStream = new System.IO.MemoryStream();//执行压缩图片ImageHandle.CompressImgByte(1300, 1300, inStream, outStream);outStream.Position = 0;//压缩后的图片byte[] fileYaSuoByte = new byte[outStream.Length];outStream.Read(fileYaSuoByte, 0, (int)outStream.Length);File.WriteAllBytes(outFile,fileYaSuoByte);

这篇关于.net5生成图片缩略图,有旋转的缩略图生成回正图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1047842

相关文章

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

python如何生成指定文件大小

《python如何生成指定文件大小》:本文主要介绍python如何生成指定文件大小的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python生成指定文件大小方法一(速度最快)方法二(中等速度)方法三(生成可读文本文件–较慢)方法四(使用内存映射高效生成

c/c++的opencv实现图片膨胀

《c/c++的opencv实现图片膨胀》图像膨胀是形态学操作,通过结构元素扩张亮区填充孔洞、连接断开部分、加粗物体,OpenCV的cv::dilate函数实现该操作,本文就来介绍一下opencv图片... 目录什么是图像膨胀?结构元素 (KerChina编程nel)OpenCV 中的 cv::dilate() 函

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

MybatisX快速生成增删改查的方法示例

《MybatisX快速生成增删改查的方法示例》MybatisX是基于IDEA的MyBatis/MyBatis-Plus开发插件,本文主要介绍了MybatisX快速生成增删改查的方法示例,文中通过示例代... 目录1 安装2 基本功能2.1 XML跳转2.2 代码生成2.2.1 生成.xml中的sql语句头2

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现