C# 實現多張JPG 轉換為GIF格式

來源:互聯網
上載者:User

主要功能: 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);        }    }}

實現:圖片

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.