C# 实现多张JPG 转换为GIF格式

主要功能: 1.设置不同图片的分辨率为同一个值

                     2.把几张J PG或者PNG格式的图片合并为一张GIF的动态图片

自己在网上找到的第三方代码写的一个小demo 高手指点一下,不知道的可以借鉴一下。

注意:要把第三方的源码编译为动态库然后调用

第三方的源码地址:http://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET

主要实现代码:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using Gif.Components;

namespace ConvertJpgToGif
{
    public partial class Form1 : Form
    {
        string[] picSrcPath;
        string gifFilePath;
        string gifFileName;

        int delayTime;
        int newWidth;
        int newHeight;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "图片文件|*.jpg";
            dlg.Multiselect = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                picSrcPath = dlg.FileNames;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "GIF图片|*.gif";
            sfd.FilterIndex = 1;
            sfd.RestoreDirectory = true;

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                gifFilePath = sfd.FileName.ToString(); //获得文件路径
                gifFileName = gifFilePath.Substring(gifFilePath.LastIndexOf("\\") + 1); //获取文件名,不带路径
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!ConvertJpgtoGif(picSrcPath, gifFilePath,delayTime,newWidth,newHeight))
            {
                MessageBox.Show("转换失败");
            }
            else
            {
                MessageBox.Show("转换成功");
            }
        }
        public int getIntFromTextbox(TextBox tb)
        {
            string str = tb.Text.ToString();
            try
            {
                int time = Convert.ToInt32(str);
                return time;
            }
            catch
            {
                MessageBox.Show("输入错误重新输入");
                return -1;
            }

        }
        private bool ConvertJpgtoGif(string[] src, string gifPath,int time,int w,int h)
        {
             
             try
            {
                AnimatedGifEncoder el = new AnimatedGifEncoder();
                el.Start(gifPath);
                el.SetDelay(time);
                el.SetRepeat(0);
                for (int i = 0, count = src.Length; i < count; i++)
                {
                    Image img = Image.FromFile(src[i]);
                    img = ReSetPicSize(img, w, h);
                    el.AddFrame(img);
                }
                el.Finish();

                GifDecoder gifDecoder = new GifDecoder();
                gifDecoder.Read(gifPath);

                return true;
            }
            catch (Exception e1)
            {
                // MessageBox.Show(e1.Message);
                return false;
            }

        }
        private Image ReSetPicSize(Image image, int newW, int newH)
        {
            Bitmap bmp = new Bitmap(image);
            try
            {
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                // 插值算法的质量 
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), 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;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            delayTime = getIntFromTextbox(tbDelay);
            newWidth = getIntFromTextbox(tbW);
            newHeight = getIntFromTextbox(tbH);
        }
    }
}


实现:图片


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值