static void Main(string[] args) { /* create Gif */ //you should replace filepath //String [] imageFilePaths = new String[]{"c:\\01.png","c:\\02.png","c:\\03.png"}; string[] imageFilePaths = Directory.GetFiles(@"C:\Users\brush\Desktop\images"); String outputFilePath = "c:\\test.gif"; AnimatedGifEncoder e = new AnimatedGifEncoder(); MemoryStream mem = new MemoryStream(); e.Start(mem); e.SetDelay(500); //-1:no repeat,0:always repeat e.SetRepeat(0); for (int i = 0, count = imageFilePaths.Length; i < count; i++) { e.AddFrame(Image.FromFile(imageFilePaths[i])); } e.Finish(); File.WriteAllBytes("c:/users/brush/desktop/test.gif", mem.GetBuffer()); /* extract Gif */ GifDecoder gifDecoder = new GifDecoder(); gifDecoder.Read("c:/users/brush/desktop/test.gif"); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { Image frame = gifDecoder.GetFrame(i); // frame i frame.Save("c:/users/brush/desktop/" + Guid.NewGuid().ToString() + ".png", ImageFormat.Png); } }
public void TestResizeTransparentGIF89() { Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("SnCore.Tools.Tests.Images.transparent.gif"); // load original image in decoder GifDecoder originaldecoder = new GifDecoder(); originaldecoder.Read(sm); sm.Seek(0, SeekOrigin.Begin); GifDecoder.GifFrame frame = originaldecoder.GetFrame(0); Console.WriteLine("Transparency: {0} - {1}", frame.transparency, Color.FromArgb(frame.bgcolor)); // load in ThumnailBitmap Assert.IsNotNull(sm); ThumbnailBitmap b = new ThumbnailBitmap(sm); Console.WriteLine("Size: {0}x{1}", b.FullSize.Width, b.FullSize.Height); // Assert.AreEqual(b.FullSize, new Size(227, 227)); Assert.IsNotNull(b.Thumbnail); Assert.IsNotNull(b.Bitmap); // reload in decoder, verify transparency GifDecoder decoder = new GifDecoder(); decoder.Read(new MemoryStream(b.Bitmap)); frame = decoder.GetFrame(0); Console.WriteLine("Transparency: {0} - {1}", frame.transparency, Color.FromArgb(frame.bgcolor)); //FileStream f = File.Create("C:\\temp\\chefout.gif"); //f.Write(b.Bitmap, 0, b.Bitmap.Length); //f.Close(); //f = File.Create("C:\\temp\\chefthumbnail.gif"); //f.Write(b.Thumbnail, 0, b.Thumbnail.Length); //f.Close(); }
/// <summary> /// 背后事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BgWorker1_DoWork(object sender, DoWorkEventArgs e) { var type = "png"; var gifDecoder = new GifDecoder(); var directory = outpath.Text; directory += "\\"; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } //读取 gifDecoder.Read(giffile); SetTrackBarFlg(true); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { Image frame = gifDecoder.GetFrame(i); // frame i frame.Save(directory + "\\" + i.ToString("d3") + "." + type); var t = (i + 1) * 100 / count; SetTrackBarVal(t); Thread.Sleep(200); } SetTrackBarFlg(false); m_decode_flag = false; MessageBox.Show("gif解析完成"); }
static void Main(string[] args) { /* create Gif */ //you should replace filepath // String [] imageFilePaths = new String[]{"D:\\0.bmp","D:\\2.bmp","D:\\3.bmp","D:\\4.bmp","D:\\5.bmp","D:\\6.bmp","D:\\7.bmp","D:\\8.bmp","D:\\9.bmp","D:\\10.bmp","D:\\11.bmp","D:\\12.bmp","D:\\13.bmp","D:\\14.bmp","D:\\15.bmp","D:\\16.bmp","D:\\17.bmp","D:\\18.bmp","D:\\19.bmp","D:\\20.bmp","D:\\21.bmp","D:\\22.bmp","D:\\23.bmp","D:\\24.bmp","D:\\25.bmp","D:\\26.bmp","D:\\27.bmp","D:\\28.bmp","D:\\29.bmp","D:\\30.bmp","D:\\31.bmp","D:\\32.bmp","D:\\33.bmp","D:\\34.bmp","D:\\35.bmp","D:\\36.bmp","D:\\37.bmp","D:\\38.bmp"}; String[] imageFilePaths = new String[] { "D:\\0.bmp", "D:\\1.bmp", "D:\\2.bmp", "D:\\3.bmp", "D:\\4.bmp", "D:\\5.bmp" }; String outputFilePath = "D:\\test.gif"; AnimatedGifEncoder e = new AnimatedGifEncoder(); e.Start(outputFilePath); e.SetDelay(200); //-1:no repeat,0:always repeat e.SetRepeat(0); for (int i = 0, count = imageFilePaths.Length; i < count; i++) { e.AddFrame(Image.FromFile(imageFilePaths[i])); } e.Finish(); /* extract Gif */ string outputPath = "D:\\"; 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); } }
public byte[] GetWaitShot() { GifDecoder gifDecoder = new GifDecoder(); AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder(); MemoryStream outStream = new MemoryStream(); gifEncoder.SetFrameRate(10); gifEncoder.SetDelay(100); gifEncoder.SetRepeat(0); gifEncoder.SetTransparent(Color.Black); gifEncoder.Start(outStream); ResourceManager rm = new ResourceManager("webshot.serv.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly()); gifDecoder.Read(rm.GetStream("ajax_loader")); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { gifEncoder.AddFrame(gifDecoder.GetFrame(i), thumbwidth, thumbheight); } gifEncoder.Finish(); byte[] buffer = outStream.ToArray(); outStream.Close(); return(buffer); }
static void Main(string[] args) { /* create Gif */ //you should replace filepath String [] imageFilePaths = new String[] { "c:\\01.png", "c:\\02.png", "c:\\03.png" }; String outputFilePath = "c:\\test.gif"; AnimatedGifEncoder e = new AnimatedGifEncoder(); // read file as memorystream byte[] fileBytes = File.ReadAllBytes(outputFilePath); MemoryStream memStream = new MemoryStream(fileBytes); e.Start(memStream); e.SetDelay(500); //-1:no repeat,0:always repeat e.SetRepeat(0); for (int i = 0, count = imageFilePaths.Length; i < count; i++) { e.AddFrame(Image.FromFile(imageFilePaths[i])); } 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); } }
public static byte[] GetResizedImageBytesGIF89a(Stream stream, Size ts, int quality, out Size sz) { GifDecoder originalimage = new GifDecoder(); originalimage.Read(stream); sz = originalimage.GetFrameSize(); return(GetResizedImageBytesGIF89a(originalimage, stream, ts, quality)); }
/// <summary> /// 提取Gif中的没一张图片 /// </summary> public static void ExtractGif(string gifFullPath, string outputPath) { GifDecoder gifDecoder = new GifDecoder(); gifDecoder.Read(gifFullPath); 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 WriteableBitmap DecodeGif(MemoryStream stream) { stream.Seek(0, SeekOrigin.Begin); var gd = new GifDecoder(); int status = gd.Read(stream); if (status == GifDecoder.STATUS_OK) { return gd.GetImage(); } return null; }
//[STAThread] static void Main(string[] args) { /* create Gif */ //you should replace filepath string main_filepath = AppDomain.CurrentDomain.BaseDirectory + @"SamplePictures\"; String[] imageFilePaths = new String[] { main_filepath + "batman.jpg", main_filepath + "superman.jpg", main_filepath + "ironman.jpg", main_filepath + "spiderman.jpg" }; String outputFilePath = main_filepath + "test.gif"; AnimatedGifEncoder e = new AnimatedGifEncoder(); e.Start(outputFilePath); e.SetDelay(500); //-1:no repeat,0:always repeat e.SetRepeat(0); for (int i = 0, count = imageFilePaths.Length; i < count; i++) { e.AddFrame(Image.FromFile(imageFilePaths[i])); } e.Finish(); /* extract Gif */ string outputPath = main_filepath; GifDecoder gifDecoder = new GifDecoder(); gifDecoder.Read(outputFilePath); List <string> temp_imageFilePaths = new List <string>(); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { Image frame = gifDecoder.GetFrame(i); // frame i string framepath = outputPath + Guid.NewGuid().ToString() + ".png"; frame.Save(framepath, ImageFormat.Png); temp_imageFilePaths.Add(framepath); } //delete temporary files for (int i = 0, count = temp_imageFilePaths.Count; i < count; i++) { FileInfo file = new FileInfo(temp_imageFilePaths[i]); if (file.Exists) { file.Delete(); } } Console.WriteLine("gif is created."); Console.ReadLine(); }
public static Bitmap[] ReadGif(Stream stream) { var decoder = new GifDecoder(); decoder.Read(stream); var count = decoder.GetFrameCount(); var array = new Bitmap[count]; for (var i = 0; i < count; i++) { array[i] = new Bitmap(decoder.GetFrame(i)); } return(array); }
public static Bitmap LoadFrameImageFromFile(string fileName, int frame) { GifDecoder decoder = new GifDecoder(); if (decoder.Read(fileName) != GifDecoder.STATUS_OK) { // in the interest of speed, if decode fails assume 1 frame return(LoadFirstImageFromFile(fileName)); } // this is a GIF file so just return the frame int at = Math.Min(Math.Abs(frame), decoder.GetFrameCount() - 1); return(decoder.GetFrame(at)); }
public void TestTooSmallGIF89() { Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("SnCore.Tools.Tests.Images.small.gif"); // load original image in decoder GifDecoder originaldecoder = new GifDecoder(); originaldecoder.Read(sm); sm.Seek(0, SeekOrigin.Begin); GifDecoder.GifFrame frame = originaldecoder.GetFrame(0); Console.WriteLine("Transparency: {0} - {1}", frame.transparency, Color.FromArgb(frame.bgcolor)); // load in ThumnailBitmap Assert.IsNotNull(sm); ThumbnailBitmap b = new ThumbnailBitmap(sm); Console.WriteLine("Size: {0}x{1}", b.FullSize.Width, b.FullSize.Height); }
public static byte[] GetThumbnailGIF89a(Stream stream, Size min, Size thumb) { long offset = stream.Position; GifDecoder decoder = new GifDecoder(); decoder.Read(stream); Size sz = decoder.GetFrameSize(); stream.Seek(offset, SeekOrigin.Begin); if (min != null && sz.Width < min.Width && sz.Height < min.Height) { throw new InvalidImageSizeException( string.Empty, sz, min); } return(GetResizedImageBytesGIF89a(decoder, stream, thumb, s_ImageQuality)); }
private void button2_Click(object sender, EventArgs e) {//read the path..................................... this.openFileDialog1.Filter = "Images (*.gif)|*.GIF|" + "All files (*.*)|*.*"; this.openFileDialog1.Multiselect = false; this.openFileDialog1.Title = "Select the Image u want to decode"; string outputPath = "c:\\rahil"; GifDecoder gifDecoder = new GifDecoder(); DialogResult dr = this.openFileDialog1.ShowDialog(); gifDecoder.Read(openFileDialog1.FileName); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { Image frame = gifDecoder.GetFrame(i); // frame i frame.Save(outputPath + i + ".png", ImageFormat.Png); } }
public static int GetFrameCountEstimateFromFile(string fileName) { if (!File.Exists(fileName)) { return(0); } GifDecoder decoder = new GifDecoder(); if (decoder.Read(fileName) != GifDecoder.STATUS_OK) { // in the interest of speed, if decode fails assume 1 frame return(1); } // this is a GIF file so just return the frame count return(decoder.GetFrameCount()); }
/// <summary> /// 把Gif文件转成Png文件,放在directory目录下 /// </summary> /// <param name="file"></param> /// <param name="directory"></param> /// <returns></returns> public static void GifToPngs(string giffile, string directory) { GifDecoder gifDecoder = new GifDecoder(); directory += "\\"; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } //读取 gifDecoder.Read(giffile); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { Image frame = gifDecoder.GetFrame(i); // frame i frame.Save(directory + "\\" + i.ToString("d2") + ".png", ImageFormat.Png); //转成jpg //frame.Save(directory + "\\" + i.ToString("d2") + ".jpg", ImageFormat.Jpeg); } }
//反转GIF动画 private void buttonReverseGIF_Click(object sender, EventArgs e) { decimal decDelayTimes = numericUpDown_DelayTimes.Value; string strFileName = this.textBox_SelectGIFPath.Text; if (strFileName.Length <= 0) { MessageBox.Show("未选择要反序的文件!请选择【选择GIF文件】"); return; } /* extract Gif */ string outputPath = Path.GetTempPath(); List <string> strList = GetFrames(strFileName, outputPath); GifDecoder gifDecoder = new GifDecoder(); gifDecoder.Read(strFileName); // int count = gifDecoder.GetFrameCount(); // List<string> strList = new List<string>(); // for (int i = 0; i < count; i++) // { // Image frame = gifDecoder.GetFrame(i); // frame i // strList[i] = outputPath + Guid.NewGuid().ToString() + ".png"; // frame.Save(strList[i], ImageFormat.Png); // frame.Dispose(); // } strList.Reverse(); String outputFilePath = strFileName + ".gif"; AnimatedGifEncoder age = new AnimatedGifEncoder(); age.Start(outputFilePath); age.SetDelay(gifDecoder.GetDelay(0)); //-1:no repeat,0:always repeat age.SetRepeat(0); for (int i = 0; i < strList.Count; i++) { age.AddFrame(Image.FromFile(strList[i])); } age.Finish(); MessageBox.Show("反序完成,文件保存在" + outputFilePath + "目录!"); }
public static IEnumerable <Bitmap> LoadSpritesFromFile(string fileName) { // We have to use this stream code because using "new Bitmap(filename)" // keeps the file open until the Bitmap is disposed FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); Bitmap loadedBmp = (Bitmap)Bitmap.FromStream(fileStream); fileStream.Close(); // Unfortunately the Bitmap.Clone method will crash later due to // a .NET bug when it's loaded from a stream. Therefore we need // to make a fresh copy. loadedBmp = Utilities.CreateCopyOfBitmapPreservingColourDepth(loadedBmp); //Bitmap loadedBmp = new Bitmap(fileName); if ((System.IO.Path.GetExtension(fileName).ToLower() == ".gif") && (loadedBmp.PixelFormat != PixelFormat.Format8bppIndexed)) { // The .NET Bitmap class has a bug, whereby it will convert // animated gifs to 32-bit when it loads them. This causes // us an issue, so use the custom GifDecoder instead when // this happens. loadedBmp.Dispose(); GifDecoder decoder = new GifDecoder(); if (decoder.Read(fileName) != GifDecoder.STATUS_OK) { throw new AGS.Types.InvalidDataException("Unable to load GIF"); } for (int i = 0; i < decoder.GetFrameCount(); i++) { yield return(decoder.GetFrame(i)); } } else { yield return(loadedBmp); } }
public static bool GifToImage(string outPath, string gifFileName) { try { /* extract Gif */ GifDecoder gifDecoder = new GifDecoder(); gifDecoder.Read(gifFileName); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { Image frame = gifDecoder.GetFrame(i); // frame i frame.Save(outPath + Guid.NewGuid().ToString() + ".png", ImageFormat.Png); frame.Dispose(); } return(true); } catch { return(false); } }
static void Main(string[] args) { /* create Gif */ //you should replace filepath string folder = "C:/Users/cardman/Documents/Visual Studio 2015/Projects/Gif/Example/bolt/"; //String [] imageFilePaths = new String[]{"G:\\01.png"}; String[] imageFilePaths = new String[] { folder + "0.png", folder + "1.png", folder + "2.png", folder + "3.png", folder + "4.png", folder + "5.png", folder + "6.png", folder + "7.png", folder + "8.png", folder + "9.png" }; //String outputFilePath = "G:\\test.gif"; String outputFilePath = folder + "bolt.gif"; AnimatedGifEncoder e = new AnimatedGifEncoder(); e.Start(outputFilePath); e.SetDelay(500); //-1:no repeat,0:always repeat e.SetRepeat(0); for (int i = 0, count = imageFilePaths.Length; i < count; i++) { e.AddFrame(Image.FromFile(imageFilePaths[i])); } e.Finish(); /* extract Gif */ //string outputPath = "G:\\"; string outputPath = folder; GifDecoder gifDecoder = new GifDecoder(); //gifDecoder.Read( "G:\\test.gif" ); gifDecoder.Read(outputFilePath); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { Image frame = gifDecoder.GetFrame(i); // frame i frame.Save(outputPath + "_" + i + ".png", ImageFormat.Png); } }
/// <summary> /// 把Gif文件转成Png文件,放在directory目录下 /// </summary> /// <param name="giffile">gif文件</param> /// <param name="directory">输出目录</param> /// <param name="type">输出类型</param> /// <returns></returns> public void GifToPngs(string giffile, string directory, string type) { if (type != "jpg" && type != "png" && type != "bmp") { var ex = new NullReferenceException(); throw ex; } var gifDecoder = new GifDecoder(); directory += "\\"; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } //读取 gifDecoder.Read(giffile); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { Image frame = gifDecoder.GetFrame(i); // frame i frame.Save(directory + "\\" + i.ToString("d3") + "." + type); } MessageBox.Show("gif解析完成"); }
public static BRESNode FromGIF(string file) { string s = Path.GetFileNameWithoutExtension(file); BRESNode b = new BRESNode() { _name = s }; PAT0Node p = new PAT0Node() { _name = s }; p.CreateEntry(); PAT0TextureNode t = p.Children[0].Children[0] as PAT0TextureNode; GifDecoder d = new GifDecoder(); d.Read(file); int f = d.GetFrameCount(); using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.Source = (Bitmap)d.GetFrame(0); if (dlg.ShowDialog(null, b) == DialogResult.OK) { for (int i = 1; i < f; i++) { dlg.Source = (Bitmap)d.GetFrame(i); dlg.EncodeSource(); } } } return(b); }
public void TestLoadGIF89a() { GifDecoder a_decoder = new GifDecoder(); Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("SnCore.Tools.Tests.Images.animated.gif"); Console.WriteLine("Bytes: {0}", sm.Length); a_decoder.Read(sm); Console.WriteLine("Frames: {0}", a_decoder.GetFrameCount()); sm.Seek(0, SeekOrigin.Begin); Assert.IsNotNull(sm); ThumbnailBitmap b = new ThumbnailBitmap(sm); Console.WriteLine("Size: {0}x{1}", b.FullSize.Width, b.FullSize.Height); Assert.AreEqual(new Size(320, 240), b.FullSize); Assert.IsNotNull(b.Thumbnail); Assert.IsNotNull(b.Bitmap); ThumbnailBitmap th = new ThumbnailBitmap(b.Thumbnail); Console.WriteLine("Size: {0}x{1}", th.FullSize.Width, th.FullSize.Height); Assert.AreEqual(new Size(150, 100), th.FullSize); // make sure that the bitmap is still animated Console.WriteLine("Bytes: {0}", b.Bitmap.Length); GifDecoder decoder = new GifDecoder(); decoder.Read(new MemoryStream(b.Bitmap)); Console.WriteLine("Frames: {0}", decoder.GetFrameCount()); GifDecoder th_decoder = new GifDecoder(); Console.WriteLine("Bytes: {0}", b.Thumbnail.Length); th_decoder.Read(new MemoryStream(b.Thumbnail)); Console.WriteLine("Frames: {0}", th_decoder.GetFrameCount()); Assert.AreEqual(th_decoder.GetFrameCount(), decoder.GetFrameCount()); }
public void ConsumeAnimatedGIFTest() { MemoryStream ms = new MemoryStream(); AnimatedGifEncoder encoder = new AnimatedGifEncoder(); // encoder.SetDelay(200); encoder.SetFrameRate(5); encoder.Start(ms); for (char i = 'a'; i <= 'z'; i++) { Console.Write(i.ToString()); encoder.AddFrame(ThumbnailBitmap.GetBitmapFromText(i.ToString(), 48, 100, 200)); } Console.WriteLine(); encoder.Finish(); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); GifDecoder decoder = new GifDecoder(); decoder.Read(ms); Console.WriteLine("Frames: {0}", decoder.GetFrameCount()); Assert.AreEqual(26, decoder.GetFrameCount()); }
private void label9_Click(object sender, EventArgs e) { if (createProjectS == 1) { if (MessageBox.Show("Создание нового проекта уничтожит существующий. Вы хотите продолжить?", "Упс", MessageBoxButtons.YesNo) == DialogResult.Yes) { openGif = true; } } if (openGif) { OpenFileDialog open_dialog = new OpenFileDialog(); open_dialog.Filter = "Image Files(*.GIF)|*.GIF"; if (open_dialog.ShowDialog() == DialogResult.OK) { label14.Enabled = true; label12.Enabled = true; label13.Enabled = true; label15.Enabled = true; label3.Enabled = true; label4.Enabled = true; label2.Enabled = true; label5.Enabled = true; label6.Enabled = true; label7.Enabled = true; label10.Enabled = true; if (createProjectS == 1) { for (int i = nomerKadra2; i > -1; i--) { nomerKadra = i; Button btn = (Button)buttonKadr[i]; Bitmap kas = bitMapList[nomerKadra]; if (flowLayoutPanel1.Controls.Contains(btn)) { btn.Click -= new EventHandler(button_Click); flowLayoutPanel1.Controls.Remove(btn); btn.Dispose(); imageList1.Images.RemoveAt(btn.TabIndex); bitMapList.Remove(kas); nomerKadra2--; } izmenenie_poryadka(); createProjectS = 0; nomerKadra2 = 0; } } outputPath = open_dialog.FileName; GifDecoder gifDecoder = new GifDecoder(); gifDecoder.Read(outputPath); createProjectS = 1; for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { Image frame = gifDecoder.GetFrame(i); //pictureBox1.Width = frame.Width; //pictureBox1.Height = frame.Height; nomerKadra = i; nomerKadra2 = nomerKadra; bitMapList.Add((Bitmap)frame); imageList1.Images.Add((Bitmap)frame); snapshot = (Bitmap)frame; tempDraw = (Bitmap)snapshot.Clone(); pictureBox1.Image = bitMapList[nomerKadra]; createButtonForKadr(); } } } }
public void ImportGIF(string file) { Action <object, DoWorkEventArgs> work = (object sender, DoWorkEventArgs e) => { GifDecoder decoder = new GifDecoder(); decoder.Read(file, null); e.Result = decoder; }; Action <object, RunWorkerCompletedEventArgs> completed = (object sender, RunWorkerCompletedEventArgs e) => { GifDecoder decoder = e.Result as GifDecoder; string s = Path.GetFileNameWithoutExtension(file); PAT0Node p = CreateResource <PAT0Node>(s); p._loop = true; p.CreateEntry(); PAT0TextureNode textureNode = p.Children[0].Children[0] as PAT0TextureNode; PAT0TextureEntryNode entry = textureNode.Children[0] as PAT0TextureEntryNode; //Get the number of images in the file int frames = decoder.GetFrameCount(); //The framecount will be created by adding up each image delay. float frameCount = 0; bool resized = false; int w = 0, h = 0; Action <int, int> onResized = (newW, newH) => { if (resized != true) { w = newW; h = newH; resized = true; } }; using (TextureConverterDialog dlg = new TextureConverterDialog()) { using (ProgressWindow progress = new ProgressWindow(RootNode._mainForm, "GIF to PAT0 converter", "Converting, please wait...", true)) { Bitmap prev = null; progress.Begin(0, frames, 0); for (int i = 0; i < frames; i++, entry = new PAT0TextureEntryNode()) { if (progress.Cancelled) { break; } string name = s + "." + i; dlg.ImageSource = name + "."; using (Bitmap img = (Bitmap)decoder.GetFrame(i)) { if (i == 0) { dlg.LoadImages(img.Copy()); dlg.Resized += onResized; if (dlg.ShowDialog(null, this) != DialogResult.OK) { return; } textureNode._hasTex = dlg.TextureData != null; textureNode._hasPlt = dlg.PaletteData != null; prev = img.Copy(); } else { //Draw the current image over the previous //This is because some GIFs use pixels of the previous frame //in order to compress the overall image data using (Graphics graphics = Graphics.FromImage(prev)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.CompositingMode = CompositingMode.SourceOver; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.DrawImage(img, 0, 0, prev.Width, prev.Height); } dlg.LoadImages(prev.Copy()); if (resized) { dlg.ResizeImage(w, h); } else { dlg.UpdatePreview(); } dlg.EncodeSource(); textureNode.AddChild(entry); } } entry._frame = (float)Math.Round(frameCount, 2); frameCount += decoder.GetDelay(i) / 1000.0f * 60.0f; if (textureNode._hasTex) { entry.Texture = name; } if (textureNode._hasPlt) { entry.Palette = name; } progress.Update(progress.CurrentValue + 1); } progress.Finish(); if (prev != null) { prev.Dispose(); } } } p._numFrames = (ushort)(frameCount + 0.5f); }; using (BackgroundWorker b = new BackgroundWorker()) { b.DoWork += new DoWorkEventHandler(work); b.RunWorkerCompleted += new RunWorkerCompletedEventHandler(completed); b.RunWorkerAsync(); } }
private void button3_Click(object sender, EventArgs e) { PowerPoint.Slide slide = app.ActiveWindow.View.Slide; string name = app.ActivePresentation.Name; if (name.Contains(".pptx")) { name = name.Replace(".pptx", ""); } if (name.Contains(".ppt")) { name = name.Replace(".ppt", ""); } string cPath = app.ActivePresentation.Path + @"\" + name + @" 的GIF图\"; string gifFile = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog(); if (!Directory.Exists(cPath)) { openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); } else { openFileDialog1.InitialDirectory = cPath; } openFileDialog1.Filter = "Image files (*.gif)|*.gif"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { gifFile = openFileDialog1.FileName; if (!Directory.Exists(cPath)) { Directory.CreateDirectory(cPath); } System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(cPath); int k = dir.GetFiles().Length + 1; GifDecoder gifDecoder = new GifDecoder(); gifDecoder.Read(gifFile); int gcount = gifDecoder.GetFrameCount(); float swidth = app.ActivePresentation.PageSetup.SlideWidth / gcount; float sheight = app.ActivePresentation.PageSetup.SlideHeight * 0.5f; Image pic = Image.FromFile(gifFile); int height = pic.Height; int width = pic.Width; pic.Dispose(); for (int j = 0; j < gcount; j++) { Image frame = gifDecoder.GetFrame(j); frame.Save(cPath + name + "_" + k + "_" + (j + 1).ToString() + ".png", ImageFormat.Png); if (checkBox3.Checked) { slide.Shapes.AddPicture(cPath + name + "_" + k + "_" + (j + 1).ToString() + ".png", Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, j * swidth, sheight, width, height); File.Delete(cPath + name + "_" + k + "_" + (j + 1).ToString() + ".png"); if (dir.GetFiles().Length == 0) { Directory.Delete(cPath, true); } } } if (!checkBox3.Checked) { System.Diagnostics.Process.Start("Explorer.exe", cPath); } } }
public static void GenerateAvatar(int userID, string stringInDatabase) { string extension = Path.GetExtension(stringInDatabase); string fileName = SetupManager.GetAvatarFilename(userID, stringInDatabase);// userID + extension; string srcPath = SetupManager.GetSrcAvatarPath(userID, stringInDatabase); string tempFileName = fileName.Replace("\\", "/").Replace(userID + "/", ""); string bigAvatarPath = IOUtil.JoinPath(bigAvatarOutputPath, tempFileName); string smallAvatarPath = IOUtil.JoinPath(smallAvatarOutputPath, tempFileName); string normalAvatarPath = IOUtil.JoinPath(normalAvatarOutputPath, tempFileName); string bigDir = bigAvatarPath.Replace("/", "\\"); bigDir = bigDir.Substring(0, bigDir.LastIndexOf("\\")); string smallDir = smallAvatarPath.Replace("/", "\\"); smallDir = smallDir.Substring(0, smallDir.LastIndexOf("\\")); string normalDir = normalAvatarPath.Replace("/", "\\"); normalDir = normalDir.Substring(0, normalDir.LastIndexOf("\\")); if (Directory.Exists(bigDir) == false) { Directory.CreateDirectory(bigDir); } if (Directory.Exists(smallDir) == false) { Directory.CreateDirectory(smallDir); } if (Directory.Exists(normalDir) == false) { Directory.CreateDirectory(normalDir); } if (string.Compare(".gif", extension, true) == 0) { GifDecoder gifDecoder = new GifDecoder(); if (gifDecoder.Read(srcPath) == GifDecoder.STATUS_OK) { GenerateThumbGif(gifDecoder, smallAvatarPath, smallAvatarSize); GenerateThumbGif(gifDecoder, normalAvatarPath, normalAvatarSize); GenerateThumbGif(gifDecoder, bigAvatarPath, bigAvatarSize); } else { using (Image image = Image.FromFile(srcPath)) { GenerateThumb(image, smallAvatarPath, smallAvatarSize); GenerateThumb(image, normalAvatarPath, normalAvatarSize); GenerateThumb(image, bigAvatarPath, bigAvatarSize); } } } else { using (Image image = Image.FromFile(srcPath)) { GenerateThumb(image, smallAvatarPath, smallAvatarSize); GenerateThumb(image, normalAvatarPath, normalAvatarSize); GenerateThumb(image, bigAvatarPath, bigAvatarSize); } } }