예제 #1
0
        //反转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 + "目录!");
        }
예제 #2
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();
            }
        }
예제 #3
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();
        }