Example #1
0
        public void Create_Gif_Img_To_Png_Test()
        {
            var isExists = File.Exists(imageGifPath);

            Assert.IsTrue(isExists, "文件存在");
            AnimatedGifDecoder de = new AnimatedGifDecoder();

            de.Read(imageGifPath);
            for (int i = 0, count = de.GetFrameCount(); i < count; i++)
            {
                System.DrawingCore.Image frame = de.GetFrame(i);

                frame.Save(outputFilePath + Guid.NewGuid().ToString() + ".png");
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="imageGifPath"></param>
        /// <param name="outputFilePath"></param>
        /// <returns></returns>
        public ReturnValues DecomposeAminmate(string imageGifPath, string outputFilePath, string fileNamePrefix = null, string FileNameSuffix = ".png")
        {
            var isExists = File.Exists(imageGifPath);

            if (!isExists)
            {
                return(new ReturnValues()
                {
                    Success = false
                });
            }
            AnimatedGifDecoder de = new AnimatedGifDecoder();

            de.Read(imageGifPath);

            List <string> list = new List <string>();

            for (int i = 0, count = de.GetFrameCount(); i < count; i++)
            {
                if (string.IsNullOrWhiteSpace(fileNamePrefix))
                {
                    fileNamePrefix = Guid.NewGuid().ToString();
                }
                outputFilePath = outputFilePath + fileNamePrefix + FileNameSuffix;
                try
                {
                    System.DrawingCore.Image frame = de.GetFrame(i);
                    frame.Save(outputFilePath);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("捕获异常,不处理" + ex.Message);
                }
                list.Add(outputFilePath);
            }
            return(new ReturnValues()
            {
                Success = list.Count == de.GetFrameCount() ? true : false,
                FrameCount = de.GetFrameCount(),
                Delay = de.GetDelay(0),
                FrameSize = de.GetFrameSize(),
                OutputFilePaths = list,
            });
        }
Example #3
0
        /// <summary>
        /// 分解GIF
        /// </summary>
        /// <param name="imagePath"></param>
        /// <param name="outputPath"></param>
        /// <param name="list"></param>
        /// <param name="fileNamePrefix"></param>
        /// <param name="fileNameSuffix"></param>
        /// <returns></returns>
        public bool SetGifImage(string imagePath, string outputPath, out List <string> list, string fileNamePrefix = null, string fileNameSuffix = ".png")
        {
            bool success = false;

            list = new List <string>();
            AnimatedGifDecoder de = new AnimatedGifDecoder();

            de.Read(imagePath);
            for (int i = 0, count = de.GetFrameCount(); i < count; i++)
            {
                Image frame          = de.GetFrame(i);
                var   saveOutputPath = outputPath + fileNamePrefix + Guid.NewGuid().ToString("N") + fileNameSuffix;

                frame.Save(saveOutputPath);
                list.Add(saveOutputPath);
                success = File.Exists(saveOutputPath) ? true : false;
            }
            return(success);
        }
Example #4
0
        /// <summary>
        /// 添加图片水印
        /// </summary>
        /// <param name="sourceImage">源图片</param>
        /// <param name="waterImage">水印图片</param>
        /// <param name="dissolve">透明度,取值范围1-100,默认值为100(完全不透明)。</param>
        /// <param name="imagePosition">水印位置</param>
        /// <param name="distanceX">横轴边距,单位:像素(px),默认值为10。</param>
        /// <param name="distanceY">纵轴边距,单位:像素(px),默认值为10。</param>
        /// <param name="watermarkScale">水印图片自适应原图的短边比例,ws的取值范围为0-1。具体是指水印图片保持原比例,并短边缩放到原图短边*ws。</param>
        /// <param name="watermarkScaleType">水印图片自适应原图的类型,取值0、1、2、3分别表示为自适应原图的短边、长边、宽、高,默认值为0</param>
        /// <returns>水印图片</returns>
        public Image SetWaterMark(Image sourceImage, Image waterImage, float dissolve = 100, ImagePosition imagePosition = ImagePosition.RigthBottom, int distanceX = 10, int distanceY = 10, int watermarkScale = 0, int watermarkScaleType = 0)
        {
            dissolve = dissolve > 100 ? 100 : dissolve;
            dissolve = dissolve < 0 ? 0 : dissolve;


            //水印图片不支持gif
            if (waterImage.RawFormat.Guid == ImageFormat.Gif.Guid)
            {
                throw new Exception("waterImage ImageFormat Gif");
            }
            if (sourceImage.RawFormat.Guid == ImageFormat.Gif.Guid)
            {
                string localPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp", Guid.NewGuid().ToString("N"));
                if (!Directory.Exists(localPath))
                {
                    Directory.CreateDirectory(localPath);
                }
                //拆分gif
                string tempGif = Path.Combine(localPath, Guid.NewGuid().ToString("N") + ".gif");
                sourceImage.Save(tempGif, ImageFormat.Gif);
                List <Info> list = new List <Info>();

                //历史保存
                AnimatedGifDecoder de = new AnimatedGifDecoder();
                de.Read(tempGif);
                var tempRepeat = de.GetLoopCount();
                for (int i = 0, count = de.GetFrameCount(); i < count; i++)
                {
                    Image frame = de.GetFrame(i);

                    string tempPng = Path.Combine(localPath, "sc_" + Guid.NewGuid().ToString("N") + ".png");
                    frame.Save(tempPng);
                    //图片添加水印
                    var img = SetWaterMarkByImg(frame, waterImage, dissolve, imagePosition, distanceX, distanceY, watermarkScale, watermarkScaleType);

                    string tempWaterPng = Path.Combine(localPath, "waterImage_" + Guid.NewGuid().ToString("N") + ".png");

                    img.Save(tempWaterPng);
                    list.Add(new Info
                    {
                        imgDelay = de.GetDelay(i),
                        imgPath  = tempWaterPng,
                    });
                }
                //拼接GIF
                AnimatedGifEncoder e1 = new AnimatedGifEncoder();
                e1.Start(tempGif);


                e1.SetRepeat(tempRepeat);  //-1:不循环,0:总是循环 播放
                foreach (var info in list)
                {
                    e1.SetDelay(info.imgDelay);
                    e1.AddFrame(Image.FromFile(info.imgPath));
                }
                e1.Finish();
                Image temp = Image.FromFile(tempGif);

                return(temp);
            }
            return(SetWaterMarkByImg(sourceImage, waterImage, dissolve, imagePosition, distanceX, distanceY, watermarkScale, watermarkScaleType));
        }