Beispiel #1
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();
        }