public static void Run() { // ExStart:ConvertGIFFImageFrame // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WebPImages(); // Load GIFF image into the instance of image class. using (Image image = Image.Load(dataDir + "asposelogo.gif")) { // Create an instance of GIFF image class. GifImage gif = image as GifImage; if (gif == null) return; // Load an existing WebP image into the instance of WebPImage class. using (WebPImage webp = new WebPImage(image.Width, image.Height, null)) { // Loop through the GIFF frames for (int i = 0; i < gif.Blocks.Length; i++) { // Convert GIFF block to GIFF Frame GifFrameBlock gifBlock = gif.Blocks[i] as GifFrameBlock; if (gifBlock == null) { continue; } // Create an instance of WebP Frame instance by passing GIFF frame to class constructor. WebPFrameBlock block = new WebPFrameBlock(gifBlock) { Top = (short)gifBlock.Top, Left = (short)gifBlock.Left, Duration = (short)gifBlock.ControlBlock.DelayTime }; // Add WebP frame to WebP image block list webp.AddBlock(block); } // Set Properties of WebP image. webp.Options.AnimBackgroundColor = 0xff; // Black webp.Options.AnimLoopCount = 0; // Infinity webp.Options.Quality = 50; webp.Options.Lossless = false; // Save WebP image. webp.Save(dataDir + "ConvertGIFFImageFrame_out.webp"); } } // ExEnd:ConvertGIFFImageFrame }
private async void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args) { if (args.InRecycleQueue) { return; } var content = args.ItemContainer.ContentTemplateRoot as Image; var sticker = args.Item as Sticker; if (sticker == null || sticker.Thumbnail == null) { content.Source = null; return; } if (args.Phase < 2) { content.Source = null; args.RegisterUpdateCallback(OnContainerContentChanging); } else { var file = sticker.Thumbnail.Photo; if (file.Local.IsDownloadingCompleted) { var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path); var buffer = await FileIO.ReadBufferAsync(temp); content.Source = WebPImage.DecodeFromBuffer(buffer); } else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive) { ViewModel.ProtoService.Send(new DownloadFile(1, file.Id)); } } args.Handled = true; }
private async void Toolbar_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args) { if (args.InRecycleQueue) { return; } var content = args.ItemContainer.ContentTemplateRoot as Image; var sticker = args.Item as ViewModels.Dialogs.StickerSetViewModel; if (content == null || sticker == null || sticker.Covers == null) { return; } var cover = sticker.Covers.FirstOrDefault(); if (cover == null || cover.Thumbnail == null) { content.Source = null; return; } var file = cover.Thumbnail.Photo; if (file.Local.IsDownloadingCompleted) { var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path); var buffer = await FileIO.ReadBufferAsync(temp); content.Source = WebPImage.DecodeFromBuffer(buffer); } else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive) { //DownloadFile(file.Id, cover); ViewModel.ProtoService.Send(new DownloadFile(file.Id, 1)); } }
public async Task <InputMessageFactory> CreateDocumentAsync(StorageFile file) { var generated = await file.ToGeneratedAsync(); var thumbnail = new InputThumbnail(await file.ToGeneratedAsync(ConversionType.DocumentThumbnail), 0, 0); if (file.FileType.Equals(".webp", StringComparison.OrdinalIgnoreCase)) { try { var buffer = await FileIO.ReadBufferAsync(file); var webp = WebPImage.DecodeFromBuffer(buffer); var width = webp.PixelWidth; var height = webp.PixelHeight; return(new InputMessageFactory { InputFile = generated, Type = new FileTypeSticker(), Delegate = (inputFile, caption) => new InputMessageSticker(inputFile, null, width, height) }); } catch { // Not really a sticker, go on sending as a file } } else if (file.FileType.Equals(".mp3", StringComparison.OrdinalIgnoreCase)) { var props = await file.Properties.GetMusicPropertiesAsync(); return(new InputMessageFactory { InputFile = generated, Type = new FileTypeAudio(), Delegate = (inputFile, caption) => new InputMessageAudio(inputFile, thumbnail, (int)props.Duration.TotalSeconds, props.Title, props.Artist, caption) }); } return(new InputMessageFactory { InputFile = generated, Type = new FileTypeDocument(), Delegate = (inputFile, caption) => new InputMessageDocument(inputFile, thumbnail, caption) }); }
public static void Run() { // ExStart:ExtractFrameFromWebPImage // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WebPImages(); // Load an existing WebP image into the instance of WebPImage class. using (WebPImage image = new WebPImage(dataDir + "asposelogo.webp")) { if (image.Blocks.Length > 2) { // Access a particular frame from WebP image and cast it to Raster Image RasterImage block = (image.Blocks[2] as RasterImage); if (block != null) { // Save the Raster Image to a BMP image. block.Save(dataDir + "ExtractFrameFromWebPImage.bmp", new BmpOptions()); } } } // ExEnd:ExtractFrameFromWebPImage }
public static void Run() { //ExStart:ExtractFrameFromWebPImage // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WebPImages(); // Load an existing WebP image into the instance of WebPImage class. using (WebPImage image = new WebPImage(dataDir + "asposelogo.webp")) { if (image.Blocks.Length > 2) { // Access a particular frame from WebP image and cast it to Raster Image RasterImage block = (image.Blocks[2] as RasterImage); if (block != null) { // Save the Raster Image to a BMP image. block.Save(dataDir + "ExtractFrameFromWebPImage.bmp", new BmpOptions()); } } } //ExEnd:ExtractFrameFromWebPImage }
private void SetWebPSource(ITLTransferable transferable, TLDocument document, int fileSize, int phase) { if (phase >= Phase && document != null) { //Phase = phase; var fileName = document.GetFileName(); if (File.Exists(FileUtils.GetTempFileName(fileName))) { //Image.UriSource = FileUtils.GetTempFileUri(fileName); _bitmapImage.SetSource(WebPImage.Encode(File.ReadAllBytes(FileUtils.GetTempFileName(fileName)))); } else { Execute.BeginOnThreadPool(async() => { var result = await _downloadFileManager.DownloadFileAsync(fileName, document.DCId, document.ToInputFileLocation(), fileSize).AsTask(transferable?.Download()); if (result != null && Phase <= phase) { Phase = phase; Execute.BeginOnUIThread(() => { if (transferable != null) { transferable.IsTransferring = false; } //Image.UriSource = FileUtils.GetTempFileUri(fileName); _bitmapImage.SetSource(WebPImage.Encode(File.ReadAllBytes(FileUtils.GetTempFileName(fileName)))); }); } }); } } }
public void Highlight() { var message = DataContext as TLMessage; if (message == null) { return; } var sticker = message.IsSticker(); var round = message.IsRoundVideo(); var target = sticker || round ? Media : (FrameworkElement)ContentPanel; var overlay = ElementCompositionPreview.GetElementChildVisual(target) as SpriteVisual; if (overlay == null) { overlay = ElementCompositionPreview.GetElementVisual(this).Compositor.CreateSpriteVisual(); ElementCompositionPreview.SetElementChildVisual(target, overlay); } var settings = new UISettings(); var fill = overlay.Compositor.CreateColorBrush(settings.GetColorValue(UIColorType.Accent)); var brush = (CompositionBrush)fill; if (sticker || round) { ImageLoader.Initialize(overlay.Compositor); ManagedSurface surface = null; if (sticker) { var document = message.GetDocument(); var fileName = document.GetFileName(); if (File.Exists(FileUtils.GetTempFileName(fileName))) { var decoded = WebPImage.Encode(File.ReadAllBytes(FileUtils.GetTempFileName(fileName))); surface = ImageLoader.Instance.LoadFromStream(decoded); } } else { surface = ImageLoader.Instance.LoadCircle(100, Windows.UI.Colors.White); } if (surface != null) { var mask = overlay.Compositor.CreateMaskBrush(); mask.Mask = surface.Brush; mask.Source = fill; brush = mask; } } overlay.Size = new System.Numerics.Vector2((float)target.ActualWidth, (float)target.ActualHeight); overlay.Opacity = 0f; overlay.Brush = brush; var animation = overlay.Compositor.CreateScalarKeyFrameAnimation(); animation.Duration = TimeSpan.FromSeconds(2); animation.InsertKeyFrame(300f / 2000f, 0.4f); animation.InsertKeyFrame(1700f / 2000f, 0.4f); animation.InsertKeyFrame(1, 0); overlay.StartAnimation("Opacity", animation); }
private async void LoadVisibleStickers() { var scrollingHost = Stickers.ItemsPanelRoot as ItemsWrapGrid; if (scrollingHost == null) { return; } if (scrollingHost.FirstVisibleIndex < 0) { return; } var lastSet = 0L; for (int i = scrollingHost.FirstVisibleIndex; i <= scrollingHost.LastVisibleIndex; i++) { if (i >= Stickers.Items.Count) { return; } var first = Stickers.Items[i] as ViewModels.Dialogs.StickerViewModel; if (first == null || first.SetId == lastSet) { continue; } lastSet = first.SetId; var fromItem = Stickers.ContainerFromItem(first); if (fromItem == null) { continue; } var header = Stickers.GroupHeaderContainerFromItemContainer(fromItem) as GridViewHeaderItem; if (header == null) { continue; } var group = header.Content as ViewModels.Dialogs.StickerSetViewModel; if (group == null || group.IsLoaded) { continue; } group.IsLoaded = true; Debug.WriteLine("Loading sticker set " + group.Id); var response = await ViewModel.ProtoService.SendAsync(new GetStickerSet(group.Id)); if (response is StickerSet full) { group.Update(full); //group.Stickers.RaiseCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset)); int j = 0; foreach (var sticker in group.Stickers) { if (sticker.Thumbnail == null) { continue; } //group.Stickers.RaiseCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Move, sticker, j, j)); //j++; var container = Stickers.ContainerFromItem(sticker) as SelectorItem; if (container == null) { continue; } var content = container.ContentTemplateRoot as Image; var file = sticker.Thumbnail.Photo; if (file.Local.IsDownloadingCompleted) { var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path); var buffer = await FileIO.ReadBufferAsync(temp); content.Source = WebPImage.DecodeFromBuffer(buffer); } else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive) { DownloadFile(file.Id, sticker); } } } } }
public async void UpdateFile(File file) { if (!file.Local.IsDownloadingCompleted) { return; } if (_stickers.TryGetValue(file.Id, out List <ViewModels.Dialogs.StickerViewModel> items) && items.Count > 0) { var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path); var buffer = await FileIO.ReadBufferAsync(temp); foreach (var item in items) { item.UpdateFile(file); var container = Stickers.ContainerFromItem(item) as SelectorItem; if (container == null) { return; } var content = container.ContentTemplateRoot as Image; content.Source = WebPImage.DecodeFromBuffer(buffer); } } foreach (MosaicMediaRow line in GifsView.Items) { var any = false; foreach (var item in line) { if (item.Item is Animation animation && animation.UpdateFile(file)) { any = true; } } if (!any) { continue; } var container = GifsView.ContainerFromItem(line) as SelectorItem; if (container == null) { continue; } var content = container.ContentTemplateRoot as MosaicRow; if (content == null) { continue; } content.UpdateFile(line, file); } foreach (ViewModels.Dialogs.StickerSetViewModel stickerSet in Toolbar.Items) { if (stickerSet.Covers == null) { continue; } var cover = stickerSet.Covers.FirstOrDefault(); if (cover == null || cover.Thumbnail == null) { continue; } var container = Toolbar.ContainerFromItem(stickerSet) as SelectorItem; if (container == null) { continue; } var content = container.ContentTemplateRoot as Image; if (content == null) { continue; } if (cover.UpdateFile(file)) { var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path); var buffer = await FileIO.ReadBufferAsync(temp); content.Source = WebPImage.DecodeFromBuffer(buffer); } } //foreach (ViewModels.Dialogs.StickerViewModel item in Stickers.Items) //{ // if (item.UpdateFile(file) && file.Local.IsDownloadingCompleted) // { // var container = Stickers.ContainerFromItem(item) as SelectorItem; // if (container == null) // { // continue; // } // var content = container.ContentTemplateRoot as Image; // if (item.Thumbnail.Photo.Id == file.Id) // { // var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path); // var buffer = await FileIO.ReadBufferAsync(temp); // content.Source = WebPImage.DecodeFromBuffer(buffer); // } // } //} }
public static ImageSource GetWebPFrame(string path) { //return null; //return new BitmapImage(new Uri("file:///" + path)); return(WebPImage.DecodeFromPath(path)); }
public static ImageSource GetWebPFrame(string path) { return(WebPImage.DecodeFromPath(path)); }