ImageHelper/Gif/GifHelper.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace Utils.ImageHelper
{
    public static class GifHelper
    {
        /// <summary>
        /// 用Image播放GIF
        /// </summary>
        /// <param name="gif">GIF图片URI</param>
        /// <param name="gifPlaySeconds">GIF图片播放一次的秒数</param>
        /// <param name="imageControl">承载GIF的Image控件</param>
        /// <param name="errorMsg">错误信息</param>
        /// <returns></returns>
        public static bool GifToImageByAnimate(Uri gif, int gifPlaySeconds,Image imageControl,out string errorMsg)
        {
            errorMsg = string.Empty;
            //确实是GIF
            if (!gif.IsFile || !gif.AbsolutePath.EndsWith(".gif"))
            {
                errorMsg = "Is not gif file!";
                return false;
            }
            try
            {
                List<BitmapFrame> frameList = new List<BitmapFrame>();
                //将gif解码
                GifBitmapDecoder decoder = new GifBitmapDecoder(gif
                    ,BitmapCreateOptions.PreservePixelFormat
                    ,BitmapCacheOption.Default);
                if (!Equals(decoder, null) && decoder.Frames != null)
                {
                    //将每一帧添加到列表
                    frameList.AddRange(decoder.Frames);
                    ObjectAnimationUsingKeyFrames objKeyAnimate = new ObjectAnimationUsingKeyFrames
                    {
                        Duration = new Duration(TimeSpan.FromSeconds(gifPlaySeconds))
                    };
                    foreach (var item in frameList)
                    {
                        DiscreteObjectKeyFrame k1_img1 = new DiscreteObjectKeyFrame(item);
                        objKeyAnimate.KeyFrames.Add(k1_img1);
                    }
                    //添加第一帧为图片的开始画面
                    imageControl.Source = frameList[0];
                    //循环动画
                    Storyboard board = new Storyboard
                    {
                        RepeatBehavior = RepeatBehavior.Forever,
                        FillBehavior = FillBehavior.HoldEnd
                    };
                    board.Children.Add(objKeyAnimate);
后面还有 17 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。