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