public override void Draw(TreeNodeAdv node, DrawContext context) { Rectangle rect = GetBounds(node, context); Image img = _gif.GetFrame(_index).Image; context.Graphics.DrawImage(img, rect.Location); }
public override void Draw(TreeNodeAdv node, DrawContext context) { if (!isInProgress(node)) { return; } Rectangle rect = GetBounds(node, context); Image img = gif.GetFrame(index).Image; context.Graphics.DrawImage(img, rect.Location); }
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 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); }
/// <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[] { "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); } }
/// <summary> /// Creates a copy of an image allowing you to set the pixel format. /// Disposing of the original is the responsibility of the user. /// <remarks> /// Unlike the native <see cref="Image.Clone"/> method this also copies animation frames. /// </remarks> /// </summary> /// <param name="source">The source image to copy.</param> /// <param name="animationProcessMode">The process mode for frames in animated images.</param> /// <param name="format">The <see cref="PixelFormat"/> to set the copied image to.</param> /// <returns> /// The <see cref="Image"/>. /// </returns> public static Image Copy(this Image source, AnimationProcessMode animationProcessMode, PixelFormat format = PixelFormat.Format32bppPArgb) { if (FormatUtilities.IsAnimated(source)) { // Read from the correct first frame when performing additional processing source.SelectActiveFrame(FrameDimension.Time, 0); // TODO: Ensure that we handle other animated types. GifDecoder decoder = new GifDecoder(source, animationProcessMode); GifEncoder encoder = new GifEncoder(null, null, decoder.LoopCount); for (int i = 0; i < decoder.FrameCount; i++) { GifFrame frame = decoder.GetFrame(source, i); frame.Image = ((Bitmap)frame.Image).Clone(new Rectangle(0, 0, frame.Image.Width, frame.Image.Height), format); ((Bitmap)frame.Image).SetResolution(source.HorizontalResolution, source.VerticalResolution); encoder.AddFrame(frame); } return(encoder.Save()); } Bitmap copy = ((Bitmap)source).Clone(new Rectangle(0, 0, source.Width, source.Height), format); copy.SetResolution(source.HorizontalResolution, source.VerticalResolution); return(copy); }
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 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> /// 提取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); } }
public static IEnumerable <Bitmap> LoadSpritesFromFile(string fileName, int start = 0) { // 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; try { loadedBmp = (Bitmap)Bitmap.FromStream(fileStream); } catch { throw new Types.InvalidDataException($"Unable to load image data from '{fileName}'"); } finally { 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(); using (GifDecoder decoder = new GifDecoder(fileName)) { for (int i = start; i < decoder.GetFrameCount(); i++) { loadedBmp = (Bitmap)decoder.GetFrame(i).Clone(); yield return(loadedBmp); loadedBmp.Dispose(); } } } else { yield return(loadedBmp); } }
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); }
//[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 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 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)); }
/// <summary> /// 释放资源 /// </summary> public void Dispose() { if (animator != null) { animator.Transformed -= TransformedEvent; animator.Stop(); animator = null; } //lock (disposeObj) //{ if (gifDecoder != null) { int size = gifDecoder.GetFrameCount(); for (int i = 0; i < size; i++) { gifDecoder.GetFrame(i).Dispose(); } gifDecoder = null; } if (gifEncoder != null) { gifEncoder.Finish(); isStart = false; gifEncoder = null; } if (brushTool != null) { brushTool.Dispose(); brushTool = null; } if (bitmap != null) { isDisposed = true; bitmap.Dispose(); bitmap = null; } if (nenoGifImg != null) { nenoGifImg.Dispose(); nenoGifImg = null; } //} }
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); }
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); } }
/// <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); } }
/// <summary> /// Creates a deep copy of an image allowing you to set the pixel format. /// Disposing of the original is the responsibility of the user. /// <remarks> /// Unlike the native <see cref="Image.Clone"/> method this also copies animation frames. /// </remarks> /// </summary> /// <param name="source">The source image to copy.</param> /// <param name="animationProcessMode">The process mode for frames in animated images.</param> /// <param name="format">The <see cref="PixelFormat"/> to set the copied image to.</param> /// <param name="preserveExifData">Whether to preserve exif metadata. Defaults to false.</param> /// <returns> /// The <see cref="Image"/>. /// </returns> public static Image Copy(this Image source, AnimationProcessMode animationProcessMode, PixelFormat format = PixelFormat.Format32bppPArgb, bool preserveExifData = false) { if (source.RawFormat.Equals(ImageFormat.Gif)) { // Read from the correct first frame when performing additional processing source.SelectActiveFrame(FrameDimension.Time, 0); var decoder = new GifDecoder(source, animationProcessMode); var encoder = new GifEncoder(null, null, decoder.LoopCount); // Have to use Octree here, there's no way to inject it. var quantizer = new OctreeQuantizer(); for (var i = 0; i < decoder.FrameCount; i++) { var frame = decoder.GetFrame(source, i); frame.Image = quantizer.Quantize(((Bitmap)frame.Image).Clone(new Rectangle(0, 0, frame.Image.Width, frame.Image.Height), format)); ((Bitmap)frame.Image).SetResolution(source.HorizontalResolution, source.VerticalResolution); encoder.AddFrame(frame); } return(encoder.Save()); } // Create a new image and copy it's pixels. var copy = new Bitmap(source.Width, source.Height, format); copy.SetResolution(source.HorizontalResolution, source.VerticalResolution); using (var graphics = Graphics.FromImage(copy)) { graphics.DrawImageUnscaled(source, 0, 0); } if (preserveExifData) { foreach (var item in source.PropertyItems) { copy.SetPropertyItem(item); } } return(copy); }
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解析完成"); }
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); } } }
private static void GenerateThumbGif(GifDecoder decoder, string thumbPath, Size thumbSize) { GifEncoder encoder = new GifEncoder(); encoder.Start(thumbPath); encoder.SetSize(thumbSize.Width, thumbSize.Height); encoder.SetRepeat(decoder.GetLoopCount()); Bitmap bitmap = new Bitmap(thumbSize.Width, thumbSize.Height); for (int i = 0; i < decoder.GetFrameCount(); i++) { encoder.SetDelay(decoder.GetDelay(i)); int dispose = decoder.GetDispose(i); Color tranColor = decoder.GetTransparent(i); if (tranColor.IsEmpty == false) { encoder.SetTransparent(decoder.GetTransparent(i)); } if (dispose != 1) { bitmap.Dispose(); bitmap = new Bitmap(thumbSize.Width, thumbSize.Height); } using (Graphics g = Graphics.FromImage(bitmap)) { if (dispose == 2) { g.FillRectangle(new SolidBrush(tranColor), 0, 0, thumbSize.Width, thumbSize.Height); } //else if(dispose == 5) //{ // g.FillRectangle(new SolidBrush(Color.Black), 0, 0, thumbSize.Width, thumbSize.Height); //} g.DrawImage(decoder.GetFrame(i), 0, 0, thumbSize.Width, thumbSize.Height); } encoder.AddFrame(bitmap); } bitmap.Dispose(); //using (Bitmap bitmap = new Bitmap(thumbSize.Width, thumbSize.Height)) //{ // for (int i = 0; i < decoder.GetFrameCount(); i++) // { // encoder.SetDelay(decoder.GetDelay(i)); // Color tran = decoder.GetTransparent(i); // using (Graphics g = Graphics.FromImage(bitmap)) // { // if (tran != Color.Empty) // { // g.FillRectangle(new SolidBrush(tran), 0, 0, thumbSize.Width, thumbSize.Height); // } // else // { // } // g.DrawImage(decoder.GetFrame(i), 0, 0, thumbSize.Width, thumbSize.Height); // } // encoder.AddFrame(bitmap); // } //} encoder.Finish(); }