C# 实现多张图片合成一张gif

此实现方式在.net web项目中实现(winform也可用,自己整理下),网上找了几个例子都不行,参考谋篇博文写的代码,刚开始生成不成功,后来发现是路径写的有问题,遂重新整理一份。效果如下图

C# 实现多张图片合成一张gif_第1张图片

 

项目结构如下图

C# 实现多张图片合成一张gif_第2张图片

项目中需要引入Gif.Components.dll插件,可从文章顶部链接免费下载,

也可从网盘直接下载:

网盘地址:https://pan.baidu.com/s/1nko3jbK6dsAedBz1DHG92A

提取码:t0u3

csdn免费下载地址:

https://download.csdn.net/download/qq_22325259/12447955

这个dll资源设置的是免费下载的,不知道为什么通过审核后就需要积分下载了。积分只能下拉选择,不能自己手动填写,最高只能选择5积分,但是通过审核后,最上面的完整代码资源竟然变成了11积分!简直太过分!严重怀疑是审核员私自修改了我的积分设置,其他博客里的资源积分都发现了与本人设置不符的情况!已多次修改。

ImageGif4.aspx页面代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ImageGif4.aspx.cs" Inherits="WebApplication1.ImageGif4" %>






    


    

 

页面ImageGif4.aspx.cs,后台代码如下

using Gif.Components;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace WebApplication1
{
    public partial class ImageGif4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                CreateGif();
            }
        }

        public void CreateGif()
        {
            //多张图片地址
            String[] imageFilePaths = new String[] { "image/jiafei1.jpg", "image/jiafei2.jpg", "image/jiafei3.jpg" };
            //生成的gif图片路径
            String gifPath = "Gif/test.gif";
            ConvertJpgToGif(imageFilePaths, gifPath, 500, 800, 600);
            //ConvertGifToJpg(gifPath);
            this.Image1.ImageUrl = "../Gif/test.gif";
        }

        /// 
        /// 一张gif图片生成多张图片
        /// 
        /// gif图片路径
        public void ConvertGifToJpg(string gifPath)
        {
            GifDecoder gifDecoder = new GifDecoder();
            //读取gif图片
            gifDecoder.Read(Server.MapPath(gifPath));  //"Gif/test.gif"
            //gif拆分的多张图片保存的路径
            string outputPath = "Gif/";
            for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++)
            {
                Image frame = gifDecoder.GetFrame(i); // frame i
                frame.Save(Server.MapPath(outputPath) + Guid.NewGuid().ToString() + ".png", ImageFormat.Png);
            }
        }

        /// 
        /// 多张图片转成一张gif图片
        /// 
        /// 图片路径,放到一个数组里面
        /// 生成的gif图片路径
        /// 每一帧图片间隔时间
        /// 生成的gif图片宽度
        /// 生成的gif图片高度
        /// 
        public bool ConvertJpgToGif(string[] imageFilePaths, string gifPath, int time, int w, int h)
        {
            try
            {
                AnimatedGifEncoder e = new AnimatedGifEncoder();
                e.Start(Server.MapPath(gifPath));
                e.SetDelay(time);
                //0:循环播放    -1:不循环播放
                e.SetRepeat(0);
                for (int i = 0, count = imageFilePaths.Length; i < count; i++)
                {
                    //e.AddFrame(Image.FromFile(Server.MapPath(imageFilePaths[i])));

                    Image img = Image.FromFile(Server.MapPath(imageFilePaths[i]));
                    //如果多张图片的高度和宽度都不一样,可以打开这个注释
                    img = ReSetPicSize(img, w, h);
                    e.AddFrame(img);
                }
                e.Finish();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// 
        /// 重新调整图片的大小,使其满足要求
        /// 
        /// 
        /// 
        /// 
        /// 
        private Image ReSetPicSize(Image image, int newWidth, int newHeight)
        {
            Bitmap bmp = new Bitmap(image);
            try
            {
                Bitmap b = new Bitmap(newWidth, newHeight);
                Graphics g = Graphics.FromImage(b);
                // 插值算法的质量 
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                g.Dispose();
                // return b;
                Image img = (Image)b;
                //  MessageBox.Show("Width"+img.Width.ToString() + "Height:" + img.Height.ToString());
                return img;
            }
            catch
            {
                return null;
            }
        }

    }
}

 

注意:此种转换方式耗时长,建议开一个新的线程

文章顶部附有源码下载链接,可直接下载源代码,但不允许白嫖哦~

 

参考文档:

https://blog.csdn.net/zxy13826134783/article/details/103100369

 

 

C# 实现多张图片合成一张gif_第3张图片

微信扫码关注公众号,一起学习进步,里面有.Net相关满满的干货,等你来拿。

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(.net,.net,c#)