C#修改GIF大小同时保持GIF仍然可动和背景透明

        /// <summary>
        /// 设置GIF大小
        /// </summary>
        /// <param name="path">图片路径</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        private void setGifSize(string path, int width, int height)
        {
            Image gif = new Bitmap(width, height);
            Image frame = new Bitmap(width, height);
            Image res = Image.FromFile(path);
            Graphics g = Graphics.FromImage(gif);
            Rectangle rg = new Rectangle(0, 0, width, height);
            Graphics gFrame = Graphics.FromImage(frame);
    
            foreach (Guid gd in res.FrameDimensionsList)
            {
                FrameDimension fd = new FrameDimension(gd);
    
//因为是缩小GIF文件所以这里要设置为Time,如果是TIFF这里要设置为PAGE,因为GIF以时间分割,TIFF为页分割
                FrameDimension f = FrameDimension.Time;
                int count = res.GetFrameCount(fd);
                ImageCodecInfo codecInfo = GetEncoder(ImageFormat.Gif);
                System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.SaveFlag;
                EncoderParameters eps = null;
    
                for (int i = 0; i < count; i++)
                {
                    res.SelectActiveFrame(f, i);
                    if (0 == i)
                    {
                            
                        g.DrawImage(res, rg);
    
                        eps = new EncoderParameters(1);
    
//第一帧需要设置为MultiFrame
    
                        eps.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.MultiFrame);
                        bindProperty(res, gif);
                        gif.Save(@"C:\tmp\test\aaa.gif", codecInfo, eps);
                    }
                    else
                    {
                            
                        gFrame.DrawImage(res, rg);
    
                        eps = new EncoderParameters(1);
    
//如果是GIF这里设置为FrameDimensionTime,如果为TIFF则设置为FrameDimensionPage
    
                        eps.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.FrameDimensionTime);
    
                        bindProperty(res, frame);
                        gif.SaveAdd(frame, eps);
                    }
                }
    
                eps = new EncoderParameters(1);
                eps.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.Flush);
                gif.SaveAdd(eps);
            }
        }
    
        /// <summary>
        /// 将源图片文件里每一帧的属性设置到新的图片对象里
        /// </summary>
        /// <param name="a">源图片帧</param>
        /// <param name="b">新的图片帧</param>
        private void bindProperty(Image a,Image b)
    
        {
    
//这个东西就是每一帧所拥有的属性,可以用GetPropertyItem方法取得这里用为完全复制原有属性所以直接赋值了
    
//顺便说一下这个属性里包含每帧间隔的秒数和透明背景调色板等设置,这里具体那个值对应那个属性大家自己在msdn搜索GetPropertyItem方法说明就有了
    
            for (int i = 0; i < a.PropertyItems.Length; i++)
            {
                b.SetPropertyItem(a.PropertyItems[i]);
            }
        }
    
        private ImageCodecInfo GetEncoder(ImageFormat format)
        {
    
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
    
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }


知识共享许可协议
《C#修改GIF大小同时保持GIF仍然可动和背景透明》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     162.97ms