示例#1
0
        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);
            }
        }
示例#2
0
        /// <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解析完成");
        }
示例#3
0
        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);
            }
        }
示例#4
0
        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);
        }
示例#5
0
        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);
            }
        }
示例#6
0
        /// <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);
            }
        }
示例#7
0
        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);
            }
        }
示例#8
0
        //[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();
        }
示例#9
0
        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);
        }
示例#10
0
        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());
        }
示例#11
0
        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());
        }
示例#12
0
        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));
        }
示例#13
0
        /// <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;
            }
            //}
        }
示例#14
0
        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);
            }
        }
示例#15
0
        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());
        }
示例#16
0
        /// <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);
            }
        }
示例#17
0
        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);
            }
        }
示例#18
0
        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);
            }
        }
示例#19
0
        /// <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解析完成");
        }
示例#20
0
        public static int GetFrameCountEstimateFromFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return(0);
            }

            int count;

            try
            {
                using (GifDecoder decoder = new GifDecoder(fileName))
                {
                    count = decoder.GetFrameCount();
                }
            }
            catch (Types.InvalidDataException)
            {
                count = 1;
            }

            return(count);
        }
示例#21
0
        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);
        }
示例#22
0
        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();
                    }
                }
            }
        }
示例#23
0
        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();
            }
        }
示例#24
0
        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);
                }
            }
        }
示例#25
0
        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();
        }