本文主要是介绍.NET5生成gif缩略图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需要在nuget安装包System.Drawing.Common
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;/// <summary>/// 生成缩略图,针对GIF图片/// </summary>/// <param name="imgPath">输入文件地址</param>/// <param name="savePath">保存文件地址</param>/// <param name="width">压缩输出宽度</param>/// <param name="height">压缩输出高度</param>public static void GifYasuo(string imgPath, string savePath, int width, int height){//原图Image img = Image.FromFile(imgPath);//不够100*100的不缩放if (img.Width > 100 && img.Height > 100){//按比例缩放 if (img.Width > width){height = width * img.Height / img.Width;}//新图第一帧Image new_img = new Bitmap(width, height);//新图其他帧Image new_imgs = new Bitmap(width, height);//新图第一帧GDI+绘图对象Graphics g_new_img = Graphics.FromImage(new_img);//新图其他帧GDI+绘图对象Graphics g_new_imgs = Graphics.FromImage(new_imgs);//配置新图第一帧GDI+绘图对象g_new_img.CompositingMode = CompositingMode.SourceCopy;g_new_img.InterpolationMode = InterpolationMode.HighQualityBicubic;g_new_img.PixelOffsetMode = PixelOffsetMode.HighQuality;g_new_img.SmoothingMode = SmoothingMode.HighQuality;g_new_img.Clear(Color.FromKnownColor(KnownColor.Transparent));//配置其他帧GDI+绘图对象g_new_imgs.CompositingMode = CompositingMode.SourceCopy;g_new_imgs.InterpolationMode = InterpolationMode.HighQualityBicubic;g_new_imgs.PixelOffsetMode = PixelOffsetMode.HighQuality;g_new_imgs.SmoothingMode = SmoothingMode.HighQuality;g_new_imgs.Clear(Color.FromKnownColor(KnownColor.Transparent));质量设置//EncoderParameters qualityEncoder = new EncoderParameters(1);//long[] qy = new long[1];//qy[0] = flag;//设置压缩的比例1-100 //var ep_quality = new EncoderParameter(Encoder.Quality, qy);//qualityEncoder.Param[0] = ep_quality;//遍历维数foreach (Guid gid in img.FrameDimensionsList){//因为是缩小GIF文件所以这里要设置为Time//如果是TIFF这里要设置为PAGEFrameDimension f = FrameDimension.Time;//获取总帧数int count = img.GetFrameCount(f);//保存标示参数Encoder encoder = Encoder.SaveFlag;//EncoderParameters ep = null;//图片编码、解码器ImageCodecInfo ici = null;//图片编码、解码器集合ImageCodecInfo[] icis = ImageCodecInfo.GetImageDecoders();//为 图片编码、解码器 对象 赋值foreach (ImageCodecInfo ic in icis){if (ic.FormatID == ImageFormat.Gif.Guid){ici = ic;break;}}//每一帧for (int c = 0; c < count; c++){//选择由维度和索引指定的帧img.SelectActiveFrame(f, c);//第一帧if (c == 0){//将原图第一帧画给新图第一帧g_new_img.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);//把振频和透明背景调色板等设置复制给新图第一帧for (int i = 0; i < img.PropertyItems.Length; i++){new_img.SetPropertyItem(img.PropertyItems[i]);}ep = new EncoderParameters(1);//第一帧需要设置为MultiFrameep.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.MultiFrame);//ep.Param[1] = eParam;//保存第一帧new_img.Save(savePath, ici, ep);}//其他帧else{//把原图的其他帧画给新图的其他帧g_new_imgs.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);//把振频和透明背景调色板等设置复制给新图第一帧for (int i = 0; i < img.PropertyItems.Length; i++){new_imgs.SetPropertyItem(img.PropertyItems[i]);}ep = new EncoderParameters(1);//如果是GIF这里设置为FrameDimensionTime//如果为TIFF则设置为FrameDimensionPage ep.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.FrameDimensionTime);//向新图添加一帧new_img.SaveAdd(new_imgs, ep);//whl添加,设置保存质量//new_img.SaveAdd(qualityEncoder);}}ep = new EncoderParameters(1);//关闭多帧文件流ep.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.Flush);new_img.SaveAdd(ep);}//whl添加,设置保存质量//图片编码、解码器ImageCodecInfo ici2 = null;//图片编码、解码器集合ImageCodecInfo[] icis2 = ImageCodecInfo.GetImageDecoders();//为 图片编码、解码器 对象 赋值foreach (ImageCodecInfo ic in icis2){if (ic.FormatID == ImageFormat.Gif.Guid){ici2 = ic;break;}}//释放文件img.Dispose();new_img.Dispose();new_imgs.Dispose();g_new_img.Dispose();g_new_imgs.Dispose();}}
这篇关于.NET5生成gif缩略图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!