.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

相关文章

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例