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

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

 

项目结构如下图

项目中需要引入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" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Image ID="Image1" runat="server" />
    </div>
    </form>
</body>
</html>

 

页面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";
        }

        /// <summary>
        /// 一张gif图片生成多张图片
        /// </summary>
        /// <param name="gifPath">gif图片路径</param>
        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);
            }
        }

        /// <summary>
        /// 多张图片转成一张gif图片
        /// </summary>
        /// <param name="imageFilePaths">图片路径,放到一个数组里面</param>
        /// <param name="gifPath">生成的gif图片路径</param>
        /// <param name="time">每一帧图片间隔时间</param>
        /// <param name="w">生成的gif图片宽度</param>
        /// <param name="h">生成的gif图片高度</param>
        /// <returns></returns>
        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;
            }
        }

        /// <summary>
        /// 重新调整图片的大小,使其满足要求
        /// </summary>
        /// <param name="image"></param>
        /// <param name="newWidth"></param>
        /// <param name="newHeight"></param>
        /// <returns></returns>
        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

 

 

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

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个程序员_zhangzhen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值