static void Main(string[] args) { if (args == null || args.Length < 1) return; /* create Gif */ //you should replace filepath var imageFilePaths = args; Array.Sort(imageFilePaths, StrCmpLogicalW); string outputFilePath = Path.GetDirectoryName(imageFilePaths[0])+@"\SHG.gif"; AnimatedGifEncoder e = new AnimatedGifEncoder(); e.Start(outputFilePath); e.SetDelay(8); //-1:no repeat,0:always repeat e.SetRepeat(0); foreach (var imageFilePath in imageFilePaths) { e.AddFrame(Image.FromFile(imageFilePath)); } e.Finish(); /* extract Gif string outputPath = "c:\\"; GifDecoder gifDecoder = new GifDecoder(); gifDecoder.Read("c:\\test.gif"); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { Image frame = gifDecoder.GetFrame(i); // frame i frame.Save(outputPath + Guid.NewGuid().ToString() + ".png", ImageFormat.Png); */ }
private void startbtn_Click(object sender, EventArgs e) { if (listBox1.Items.Count < 1) return; /* create Gif */ //you should replace filepath string outputFilePath = Path.GetDirectoryName((string)listBox1.Items[0]) + @"\SHG.gif"; AnimatedGifEncoder AGEnc = new AnimatedGifEncoder(); AGEnc.Start(outputFilePath); AGEnc.SetDelay((int)delay.Value); //-1:no repeat,0:always repeat AGEnc.SetRepeat((int)RepeatNum.Value); List<BmpAndPath> Images = new List<BmpAndPath>(listBox1.Items.Count); foreach (string imageFilePath in listBox1.Items) { try { Bitmap bmp = new Bitmap(imageFilePath); Images.Add(new BmpAndPath(bmp, imageFilePath)); } catch { } } if (resize.Checked && ((int)width.Value * (int)height.Value) != 0) { Rectangle rect = new Rectangle((int)Xcoordinate.Value, (int)Ycoordinate.Value, (int)width.Value, (int)height.Value); List<BmpAndPath> OldImages = Images; Images = OldImages.ConvertAll<BmpAndPath>(bmpOld => { Bitmap bmpNew = new Bitmap(rect.Width, rect.Height); using (Graphics g = Graphics.FromImage(bmpNew)) { g.DrawImage(bmpOld.bmp, 0, 0, rect, GraphicsUnit.Pixel); if (TimeInsert.Checked) { TimeOutput(g, bmpOld.path, rect.Width, rect.Height); } } return new BmpAndPath(bmpNew, bmpOld.path); }); foreach (var img in OldImages) { img.bmp.Dispose(); } } foreach (var img in Images) { AGEnc.AddFrame(img.bmp); } AGEnc.Finish(); foreach (var img in Images) { img.bmp.Dispose(); } }