/// <summary> /// 合成GIF传入合成Gif的静态图片信息的集合对象,包括每帧的地址和每帧的播放间隔, /// 需要指定要用图片信息集合对象里面的Uri还是Stream,默认是Uri /// animatedRepeat为重复次数默认是循环(1表示只动一次,0:表示循环,n:表示循环n次), /// 如果成功返回true,失败则返回false.[非后台线程方法,会阻碍前台UI] /// </summary> /// <param name="pathList">合成GIF传入合成Gif的静态图片信息的集合对象,包括每帧的地址和每帧的播放间隔</param> /// <param name="gifDataSource">指定要用图片信息集合对象里面的Uri还是Stream,默认是Uri</param> /// <param name="animatedRepeat">animatedRepeat为重复次数默认是循环(1表示只动一次,0:表示循环,n:表示循环n次)</param> /// <param name="fileName">临时文件名,默认为"test.gif"</param> /// <returns>如果成功返回true,失败则返回false.</returns> public async static Task <bool> SynthesisGif(List <GifInformation> pathList, GifDataSource gifDataSource = GifDataSource.UriDataSource, int animatedRepeat = 0, string fileName = "test.gif") { try { if (pathList != null) { IRandomAccessStream streamRandom = null; AnimatedGifEncoder e = new AnimatedGifEncoder(); await e.CreateImageFolder(); //1表示只动一次,0:表示循环,n:表示循环n次 e.SetRepeat(animatedRepeat); //e.SetTransparent(Colors.Transparent); e.SetTransparent(Color.FromArgb(0, 0, 0, 0)); for (int i = 0; i < pathList.Count; i++) { //图片转换时间 e.SetDelay(pathList[i].DelayMilliseconds); if (gifDataSource == GifDataSource.UriDataSource) { //WriteableBitmap aa = await (new WriteableBitmap(1, 1).FromContent(pathList[i].Uri)); var rass = RandomAccessStreamReference.CreateFromUri(pathList[i].Uri); streamRandom = await rass.OpenReadAsync(); ////System.IO 为扩展命名空间 //WriteableBitmap aa = await (new WriteableBitmap(1, 1).FromStream(streamRandom.GetInputStreamAt(0).AsStreamForRead())); /*var streamRandomCopy = streamRandom.CloneStream(); * * WriteableBitmap aa = new WriteableBitmap(1, 1); * await aa.SetSourceAsync(streamRandom); * await e.AddFrame(aa, streamRandomCopy);*/ } else { streamRandom = pathList[i].IRandomAccessStream; } await e.AddFrame(streamRandom); } return(await e.Finish(fileName)); } else { return(false); } } catch (Exception e) { Debug.WriteLine(e.Message); return(false); } }
/// <summary> /// 合成GIF传入合成Gif的静态图片信息的集合对象,包括每帧的地址和每帧的播放间隔, /// animatedRepeat为重复次数默认是循环(1表示只动一次,0:表示循环,n:表示循环n次), /// 如果保存成功返回true,保存失败则返回false.[后台线程方法,不会阻碍前台UI] /// </summary> /// <param name="pathList">合成GIF传入合成Gif的静态图片信息的集合对象,包括每帧的地址和每帧的播放间隔</param> /// <param name="gifDataSource">指定要用图片信息集合对象里面的Uri还是Stream,默认是Uri</param> /// <param name="animatedRepeat">animatedRepeat为重复次数默认是循环(1表示只动一次,0:表示循环,n:表示循环n次)</param> /// <param name="fileName">临时文件名,默认为"test.gif"</param> /// <returns>如果保存成功返回true,保存失败则返回false.</returns> public async static Task <bool> SaveSynthesisGif(List <GifInformation> pathList, GifDataSource gifDataSource = GifDataSource.UriDataSource, int animatedRepeat = 0, string fileName = "test.gif") { bool ok = false; await Task.Run(async() => { if ((await SynthesisGif(pathList, gifDataSource, animatedRepeat, fileName))) { //进行保存 await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, async() => { try { StorageFolder storageFolder = await ControlLibrary.CacheManagement.CacheManagement.Instance.GetImageFolder(); StorageFile storageFile = await storageFolder.GetFileAsync(fileName); if (storageFile != null) { FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; savePicker.FileTypeChoices.Add("图片类型", new List <string>() { ".gif" }); savePicker.SuggestedFileName = storageFile.Name; StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { CachedFileManager.DeferUpdates(file); await storageFile.CopyAndReplaceAsync(file); //, file.Name, NameCollisionOption.GenerateUniqueName); FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); if (status == FileUpdateStatus.Complete) { ok = true; } } } } catch { ok = false; } }); } }); return(ok); }