.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

相关文章

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)

《Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)》本文介绍了如何使用Python和Selenium结合ddddocr库实现图片验证码的识别和点击功能,感兴趣的朋友一起看... 目录1.获取图片2.目标识别3.背景坐标识别3.1 ddddocr3.2 打码平台4.坐标点击5.图

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Python利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

Java实战之自助进行多张图片合成拼接

《Java实战之自助进行多张图片合成拼接》在当今数字化时代,图像处理技术在各个领域都发挥着至关重要的作用,本文为大家详细介绍了如何使用Java实现多张图片合成拼接,需要的可以了解下... 目录前言一、图片合成需求描述二、图片合成设计与实现1、编程语言2、基础数据准备3、图片合成流程4、图片合成实现三、总结前

Java使用POI-TL和JFreeChart动态生成Word报告

《Java使用POI-TL和JFreeChart动态生成Word报告》本文介绍了使用POI-TL和JFreeChart生成包含动态数据和图表的Word报告的方法,并分享了实际开发中的踩坑经验,通过代码... 目录前言一、需求背景二、方案分析三、 POI-TL + JFreeChart 实现3.1 Maven

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas