.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实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使