��GifImage - ����Gif����
示例#1
0
 internal static void Encode(GifImage gifImage, string gifPath)
 {
     FileStream fs = null;
     try
     {
         fs = new FileStream(gifPath, FileMode.Create);
         StreamHelper streamHelper = new StreamHelper(fs);
         streamHelper.WriteHeader(gifImage.Header);
         streamHelper.WriteLSD(gifImage.LogicalScreenDescriptor);
         if (gifImage.LogicalScreenDescriptor.GlobalColorTableFlag)
         {
             streamHelper.SetGlobalColorTable(gifImage.GlobalColorTable);
         }
         streamHelper.SetApplicationExtensions(gifImage.ApplictionExtensions);
         streamHelper.SetCommentExtensions(gifImage.CommentExtensions);
         SetFrames(gifImage.Frames, streamHelper, fs);
     }
     catch
     {
         throw;
     }
     finally
     {
         if (fs != null)
         {
             fs.Close();
         }
     }
 }
示例#2
0
        internal static GifImage Decode(Stream stream)
        {
            StreamHelper     streamHelper = null;
            GifImage         gifImage     = new GifImage();
            List <GraphicEx> graphics     = new List <GraphicEx>();
            int frameCount = 0;

            try
            {
                streamHelper = new StreamHelper(stream);
                //读取文件头
                gifImage.Header = streamHelper.ReadString(6);
                //读取逻辑屏幕标示符
                gifImage.LogicalScreenDescriptor = streamHelper.GetLCD(stream);
                if (gifImage.LogicalScreenDescriptor.GlobalColorTableFlag)
                {
                    //读取全局颜色列表
                    gifImage.GlobalColorTable = streamHelper.ReadByte(gifImage.LogicalScreenDescriptor.GlobalColorTableSize * 3);
                }
                int nextFlag = streamHelper.Read();
                while (nextFlag != 0)
                {
                    if (nextFlag == GifExtensions.ImageLabel)
                    {
                        ReadImage(streamHelper, stream, gifImage, graphics, frameCount);
                        frameCount++;
                    }
                    else if (nextFlag == GifExtensions.ExtensionIntroducer)
                    {
                        int gcl = streamHelper.Read();
                        switch (gcl)
                        {
                        case GifExtensions.GraphicControlLabel:
                        {
                            GraphicEx graphicEx = streamHelper.GetGraphicControlExtension(stream);
                            graphics.Add(graphicEx);
                            break;
                        }

                        case GifExtensions.CommentLabel:
                        {
                            CommentEx comment = streamHelper.GetCommentEx(stream);
                            gifImage.CommentExtensions.Add(comment);
                            break;
                        }

                        case GifExtensions.ApplicationExtensionLabel:
                        {
                            ApplicationEx applicationEx = streamHelper.GetApplicationEx(stream);
                            gifImage.ApplictionExtensions.Add(applicationEx);
                            break;
                        }

                        case GifExtensions.PlainTextLabel:
                        {
                            PlainTextEx textEx = streamHelper.GetPlainTextEx(stream);
                            gifImage.PlainTextEntensions.Add(textEx);
                            break;
                        }
                        }
                    }
                    else if (nextFlag == GifExtensions.EndIntroducer)
                    {
                        //到了文件尾
                        break;
                    }
                    nextFlag = streamHelper.Read();
                }

                gifImage.Texture = new Texture2D(gifImage.Width, gifImage.Height, TextureFormat.ARGB32, false);
            }
            catch
            {
                throw;
            }
            finally
            {
                stream.Close();
            }
            return(gifImage);
        }
示例#3
0
        /// <summary>
        /// 对gif动画添加水印
        /// </summary>
        /// <param name="gifFilePath">原gif动画的路径</param>
        /// <param name="text">水印文字</param>
        /// <param name="textForceColor">水印文字的颜色,因为gif不是真彩色图片,所以在显示的时候,该颜色可能有所误差,但基本上可以确定颜色范围</param>
        /// <param name="font">字体</param>
        /// <param name="x">水印位置横坐标</param>
        /// <param name="y">水印位置纵坐标</param>
        /// <param name="outputPath">输出路径</param>
        public static void WaterMark(string gifFilePath, SizeMode sizeMode, string text, Color textForceColor, Font font, float x, float y, string outputPath)
        {
            if (!File.Exists(gifFilePath))
            {
                throw new IOException(string.Format("文件{0}不存在!", gifFilePath));
            }
            using (Bitmap ora_Img = new Bitmap(gifFilePath))
            {
                if (ora_Img.RawFormat.Guid != ImageFormat.Gif.Guid)
                {
                    throw new IOException(string.Format("文件{0}!", gifFilePath));
                }
            }
            GifImage gifImage = GifDecoder.Decode(gifFilePath);

            if (sizeMode == SizeMode.Large)
            {
                ThinkDisposalMethod(gifImage);
            }
            Color textColor  = textForceColor;
            int   frameCount = 0;

            foreach (GifFrame f in gifImage.Frames)
            {
                if ((sizeMode == SizeMode.Normal && frameCount++ == 0) || sizeMode == SizeMode.Large)
                {
                    Graphics g = Graphics.FromImage(f.Image);
                    g.DrawString(text, font, new SolidBrush(textColor), new PointF(x, y));
                    g.Dispose();
                    bool      hasTextColor = false;
                    Color32[] colors       = PaletteHelper.GetColor32s(f.LocalColorTable);
                    foreach (Color32 c in colors)
                    {
                        if (c.ARGB == textColor.ToArgb())
                        {
                            hasTextColor = true;
                            break;
                        }
                    }
                    if (!hasTextColor)
                    {
                        if (f.Palette.Length < 256)
                        {
                            int       newSize   = f.Palette.Length * 2;
                            Color32[] newColors = new Color32[newSize];
                            newColors[f.Palette.Length] = new Color32(textColor.ToArgb());
                            Array.Copy(colors, newColors, colors.Length);
                            byte[] lct   = new byte[newColors.Length * 3];
                            int    index = 0;
                            foreach (Color32 c in newColors)
                            {
                                lct[index++] = c.Red;
                                lct[index++] = c.Green;
                                lct[index++] = c.Blue;
                            }
                            f.LocalColorTable         = lct;
                            f.ImageDescriptor.LctFlag = true;
                            f.ImageDescriptor.LctSize = newSize;
                            f.ColorDepth = f.ColorDepth + 1;
                        }
                        else
                        {
                            OcTreeQuantizer q          = new OcTreeQuantizer(8);
                            Color32[]       cs         = q.Quantizer(f.Image);
                            byte[]          lct        = new byte[cs.Length * 3];
                            int             index      = 0;
                            int             colorCount = 0;
                            foreach (Color32 c in cs)
                            {
                                lct[index++] = c.Red;
                                lct[index++] = c.Green;
                                lct[index++] = c.Blue;
                                if (c.ARGB == f.BgColor.ARGB)
                                {
                                    f.GraphicExtension.TranIndex = (byte)colorCount;
                                }
                                colorCount++;
                            }
                            Quantizer(f.Image, cs);
                            f.LocalColorTable         = lct;
                            f.ImageDescriptor.LctFlag = true;
                            f.ImageDescriptor.LctSize = 256;
                            f.ColorDepth = 8;
                        }
                    }
                }
            }
            GifEncoder.Encode(gifImage, outputPath);
        }
示例#4
0
        /// <summary>
        /// 合并多个gif动画,在空间坐标上
        /// </summary>
        /// <param name="sourceGifs">原图像</param>
        /// <param name="outPath">合并后图像</param>
        public static void Merge(List <string> sourceGifs, string outPath)
        {
            List <List <GifFrame> > frames = new List <List <GifFrame> >();

            foreach (string source in sourceGifs)
            {
                if (!File.Exists(source))
                {
                    throw new IOException(string.Format("文件{0}不存在!", source));
                }
                using (Bitmap ora_Img = new Bitmap(source))
                {
                    if (ora_Img.RawFormat.Guid != ImageFormat.Gif.Guid)
                    {
                        throw new IOException(string.Format("文件{0}!", source));
                    }
                }
                GifImage gifImage = GifDecoder.Decode(source);
                ThinkDisposalMethod(gifImage);
                int index = 0;
                foreach (GifFrame f in gifImage.Frames)
                {
                    if (frames.Count <= index)
                    {
                        List <GifFrame> list = new List <GifFrame>();
                        frames.Add(list);
                    }
                    List <GifFrame> frameList = frames[index];
                    frameList.Add(f);
                    index++;
                }
            }
            List <GifFrame> frameCol   = new List <GifFrame>();
            int             frameIndex = 0;

            foreach (List <GifFrame> fs in frames)
            {
                GifFrame frame = Merge(fs);
                frameCol.Add(frame);
                if (frame.Image.Width != frameCol[0].Image.Width ||
                    frame.Image.Height != frameCol[0].Image.Height)
                {
                    frame.ImageDescriptor.XOffSet         = frames[frameIndex][0].ImageDescriptor.XOffSet;
                    frame.ImageDescriptor.YOffSet         = frames[frameIndex][0].ImageDescriptor.YOffSet;
                    frame.GraphicExtension.DisposalMethod = frames[frameIndex][0].GraphicExtension.DisposalMethod;
                }
                frame.GraphicExtension.Delay = frame.Delay = frames[frameIndex][0].Delay;
                frameIndex++;
            }
            GifImage gifImg = new GifImage();

            gifImg.Header = "GIF89a";
            LogicalScreenDescriptor lcd = new LogicalScreenDescriptor();

            lcd.Width  = (short)frameCol[0].Image.Width;
            lcd.Height = (short)frameCol[0].Image.Height;
            gifImg.LogicalScreenDescriptor = lcd;
            ApplicationEx        ape  = new ApplicationEx();
            List <ApplicationEx> apps = new List <ApplicationEx>();

            apps.Add(ape);
            gifImg.ApplictionExtensions = apps;
            gifImg.Frames = frameCol;
            GifEncoder.Encode(gifImg, outPath);
        }
示例#5
0
        /// <summary>
        /// 合并多个gif文件
        /// </summary>
        /// <param name="sourceGifs">原图像路径集合</param>
        /// <param name="outGif">合并后图像路径</param>
        /// <param name="delay">间隔时间</param>
        /// <param name="repeat">是否重复播放</param>
        public static void Merge(List <string> sourceGifs, string outGif, short delay, bool repeat)
        {
            GifImage gifImage  = null;
            int      index     = 0;
            short    lastDelay = delay;

            foreach (string source in sourceGifs)
            {
                if (!File.Exists(source))
                {
                    throw new IOException(string.Format("文件{0}不存在!", source));
                }
                using (Bitmap ora_Img = new Bitmap(source))
                {
                    if (ora_Img.RawFormat.Guid != ImageFormat.Gif.Guid)
                    {
                        throw new IOException(string.Format("文件{0}!", source));
                    }
                }
                GifImage gif = GifDecoder.Decode(source);
                if (index == 0)
                {
                    gifImage = gif;
                }
                int frameCount = 0;
                foreach (GifFrame f in gif.Frames)
                {
                    if (frameCount == 0 && f.GraphicExtension.DisposalMethod == 0)
                    {
                        f.GraphicExtension.DisposalMethod = 2;
                    }
                    if (!f.ImageDescriptor.LctFlag)
                    {
                        f.ImageDescriptor.LctSize    = f.LocalColorTable.Length / 3;
                        f.ImageDescriptor.LctFlag    = true;
                        f.GraphicExtension.TranIndex = gif.LogicalScreenDescriptor.BgColorIndex;
                        f.LocalColorTable            = gif.GlobalColorTable;
                    }
                    if (frameCount == 0)
                    {
                        f.Delay = f.GraphicExtension.Delay = lastDelay;
                    }
                    if (f.Delay == 0)
                    {
                        f.Delay = f.GraphicExtension.Delay = lastDelay;
                    }
                    f.ColorDepth = (byte)(Math.Log(f.ImageDescriptor.LctSize, 2));
                    lastDelay    = f.GraphicExtension.Delay;
                    frameCount++;
                }
                gifImage.Frames.AddRange(gif.Frames);
                index++;
            }

            if (repeat && gifImage.ApplictionExtensions.Count == 0)
            {
                ApplicationEx ae = new ApplicationEx();
                gifImage.ApplictionExtensions.Add(ae);
            }
            gifImage.LogicalScreenDescriptor.PixcelAspect = 0;
            Size maxSize = FindMaxSize(sourceGifs);

            gifImage.LogicalScreenDescriptor.Width  = (short)maxSize.Width;
            gifImage.LogicalScreenDescriptor.Height = (short)maxSize.Height;
            GifEncoder.Encode(gifImage, outGif);
        }
示例#6
0
        /// <summary>
        /// Gif动画单色化
        /// </summary>
        /// <param name="gifFilePath">原动画路径</param>
        /// <param name="outputPath">单色后动画路径</param>
        public static void Monochrome(string gifFilePath, string outputPath)
        {
            if (!File.Exists(gifFilePath))
            {
                throw new IOException(string.Format("文件{0}不存在!", gifFilePath));
            }
            using (Bitmap ora_Img = new Bitmap(gifFilePath))
            {
                if (ora_Img.RawFormat.Guid != ImageFormat.Gif.Guid)
                {
                    throw new IOException(string.Format("文件{0}!", gifFilePath));
                }
            }
            GifImage gifImage   = GifDecoder.Decode(gifFilePath);
            int      transIndex = gifImage.LogicalScreenDescriptor.BgColorIndex;
            int      c1         = (255 << 24) | (158 << 16) | (128 << 8) | 128;
            int      c2         = (255 << 24) | (0 << 16) | (0 << 8) | 0;
            int      c3         = (255 << 24) | (255 << 16) | (255 << 8) | 255;
            int      c4         = (255 << 24) | (0 << 16) | (0 << 8) | 0;

            int[]  palette = new int[] { c1, c2, c3, c4 };
            byte[] buffer  = new byte[12] {
                128, 128, 128, 0, 0, 0, 255, 255, 255, 0, 0, 0
            };
            gifImage.GlobalColorTable = buffer;
            gifImage.LogicalScreenDescriptor.BgColorIndex         = 0;
            gifImage.LogicalScreenDescriptor.GlobalColorTableSize = 4;
            gifImage.LogicalScreenDescriptor.GlobalColorTableFlag = true;
            int index = 0;

            foreach (GifFrame f in gifImage.Frames)
            {
                Color32[] act = PaletteHelper.GetColor32s(f.LocalColorTable);
                f.LocalColorTable = buffer;
                Color      bgC     = act[(transIndex / 3)].Color;
                byte       bgGray  = (byte)(bgC.R * 0.3 + bgC.G * 0.59 + bgC.B * 0.11);
                BitmapData bmpData = f.Image.LockBits(new Rectangle(0, 0, f.Image.Width, f.Image.Height), ImageLockMode.ReadWrite, f.Image.PixelFormat);
                unsafe
                {
                    int *p = (int *)bmpData.Scan0.ToPointer();
                    for (int i = 0; i < f.Image.Width * f.Image.Height; i++)
                    {
                        if (p[i] == 0)
                        {
                            p[i] = c1;
                        }
                        else
                        {
                            Color c    = Color.FromArgb(p[i]);
                            int   gray = (byte)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
                            if (gray > bgGray)
                            {
                                if (bgGray > 128)
                                {
                                    p[i] = c2;
                                }
                                else
                                {
                                    p[i] = c3;
                                }
                            }
                            else if (gray < bgGray)
                            {
                                if (bgGray > 128)
                                {
                                    p[i] = c3;
                                }
                                else
                                {
                                    p[i] = c2;
                                }
                            }
                            else
                            {
                                p[i] = c1;
                            }
                        }
                    }
                }
                f.Image.UnlockBits(bmpData);
                f.GraphicExtension.TranIndex = 0;
                f.ColorDepth = 2;
                f.ImageDescriptor.LctFlag = false;
                index++;
            }
            GifEncoder.Encode(gifImage, outputPath);
        }
示例#7
0
 /// <summary>
 /// ��gifͼ���ļ����н���
 /// </summary>
 /// <param name="gifPath">gif�ļ�·��</param>
 internal static GifImage Decode(string gifPath)
 {
     FileStream fs=null;
     StreamHelper streamHelper=null;
     GifImage gifImage = new GifImage();
     List<GraphicEx> graphics = new List<GraphicEx>();
     int frameCount = 0;
     try
     {
         fs = new FileStream(gifPath,FileMode.Open);
         streamHelper = new StreamHelper(fs);
         //��ȡ�ļ�ͷ
         gifImage.Header = streamHelper.ReadString(6);
         //��ȡ�߼���Ļ��ʾ��
         gifImage.LogicalScreenDescriptor = streamHelper.GetLCD(fs);
         if (gifImage.LogicalScreenDescriptor.GlobalColorTableFlag)
         {
             //��ȡȫ����ɫ�б�
             gifImage.GlobalColorTable = streamHelper.ReadByte(gifImage.LogicalScreenDescriptor.GlobalColorTableSize * 3);
         }
         int nextFlag = streamHelper.Read();
         while (nextFlag != 0)
         {
             if (nextFlag == GifExtensions.ImageLabel)
             {
                 ReadImage(streamHelper, fs, gifImage, graphics, frameCount);
                 frameCount++;
             }
             else if (nextFlag == GifExtensions.ExtensionIntroducer)
             {
                 int gcl = streamHelper.Read();
                 switch (gcl)
                 {
                     case GifExtensions.GraphicControlLabel:
                         {
                             GraphicEx graphicEx = streamHelper.GetGraphicControlExtension(fs);
                             graphics.Add(graphicEx);
                             break;
                         }
                     case GifExtensions.CommentLabel:
                         {
                             CommentEx comment = streamHelper.GetCommentEx(fs);
                             gifImage.CommentExtensions.Add(comment);
                             break;
                         }
                     case GifExtensions.ApplicationExtensionLabel:
                         {
                             ApplicationEx applicationEx = streamHelper.GetApplicationEx(fs);
                             gifImage.ApplictionExtensions.Add(applicationEx);
                             break;
                         }
                     case GifExtensions.PlainTextLabel:
                         {
                             PlainTextEx textEx = streamHelper.GetPlainTextEx(fs);
                             gifImage.PlainTextEntensions.Add(textEx);
                             break;
                         }
                 }
             }
             else if (nextFlag == GifExtensions.EndIntroducer)
             {
                 //�����ļ�β
                 break;
             }
             nextFlag = streamHelper.Read();
         }
     }
     catch
     {
         throw;
     }
     finally
     {
         fs.Close();
     }
     return gifImage;
 }
示例#8
0
 static void ReadImage(StreamHelper streamHelper, Stream fs, GifImage gifImage, List<GraphicEx> graphics, int frameCount)
 {
     ImageDescriptor imgDes = streamHelper.GetImageDescriptor(fs);
     GifFrame frame = new GifFrame();
     frame.ImageDescriptor = imgDes;
     frame.LocalColorTable = gifImage.GlobalColorTable;
     if (imgDes.LctFlag)
     {
         frame.LocalColorTable = streamHelper.ReadByte(imgDes.LctSize*3);
     }
     LZWDecoder lzwDecoder = new LZWDecoder(fs);
     int dataSize = streamHelper.Read();
     frame.ColorDepth = dataSize;
     byte[] piexel = lzwDecoder.DecodeImageData(imgDes.Width, imgDes.Height, dataSize);
     frame.IndexedPixel = piexel;
     int blockSize = streamHelper.Read();
     DataStruct data = new DataStruct(blockSize, fs);
     GraphicEx graphicEx = graphics[frameCount];
     frame.GraphicExtension = graphicEx;
     Bitmap img = GetImageFromPixel(piexel, frame.Palette, imgDes.InterlaceFlag, imgDes.Width, imgDes.Height);
     frame.Image = img;
     gifImage.Frames.Add(frame);
 }
示例#9
0
        /// <summary>
        /// 对gif图像文件进行解码
        /// </summary>
        /// <param name="gifPath">gif文件路径</param>
        public static GifImage Decode(string gifPath)
        {
            FileStream       fs           = null;
            StreamHelper     streamHelper = null;
            GifImage         gifImage     = new GifImage();
            List <GraphicEx> graphics     = new List <GraphicEx>();
            int frameCount = 0;

//            try
//            {
            fs           = new FileStream(gifPath, FileMode.Open);
            streamHelper = new StreamHelper(fs);
            //读取文件头
            gifImage.Header = streamHelper.ReadString(6);
            //读取逻辑屏幕标示符
            gifImage.LogicalScreenDescriptor = streamHelper.GetLCD(fs);
            if (gifImage.LogicalScreenDescriptor.GlobalColorTableFlag)
            {
                //读取全局颜色列表
                gifImage.GlobalColorTable = streamHelper.ReadByte(gifImage.LogicalScreenDescriptor.GlobalColorTableSize * 3);
            }
            int nextFlag = streamHelper.Read();

            while (nextFlag != 0)
            {
                if (nextFlag == GifExtensions.ImageLabel)
                {
                    ReadImage(streamHelper, fs, gifImage, graphics, frameCount);
                    frameCount++;
                }
                else if (nextFlag == GifExtensions.ExtensionIntroducer)
                {
                    int gcl = streamHelper.Read();
                    switch (gcl)
                    {
                    case GifExtensions.GraphicControlLabel:
                    {
                        GraphicEx graphicEx = streamHelper.GetGraphicControlExtension(fs);
                        graphics.Add(graphicEx);
                        break;
                    }

                    case GifExtensions.CommentLabel:
                    {
                        CommentEx comment = streamHelper.GetCommentEx(fs);
                        gifImage.CommentExtensions.Add(comment);
                        break;
                    }

                    case GifExtensions.ApplicationExtensionLabel:
                    {
                        ApplicationEx applicationEx = streamHelper.GetApplicationEx(fs);
                        gifImage.ApplictionExtensions.Add(applicationEx);
                        break;
                    }

                    case GifExtensions.PlainTextLabel:
                    {
                        PlainTextEx textEx = streamHelper.GetPlainTextEx(fs);
                        gifImage.PlainTextEntensions.Add(textEx);
                        break;
                    }
                    }
                }
                else if (nextFlag == GifExtensions.EndIntroducer)
                {
                    //到了文件尾
                    break;
                }
                nextFlag = streamHelper.Read();
            }
//            }
//            catch
//            {
//                throw;
//            }
//            finally
//            {
            fs.Close();
//            }
            return(gifImage);
        }