public override async Task InvokeAsync(ILoadingContext <ICompositionSurface> context, PipeDelegate <ICompositionSurface> next, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (context.Current is Stream stream)
            {
                var tcs          = new TaskCompletionSource <LoadedImageSurface>();
                var imageSurface = LoadedImageSurface.StartLoadFromStream(stream.AsRandomAccessStream());

                TypedEventHandler <LoadedImageSurface, LoadedImageSourceLoadCompletedEventArgs> handler = null;
                handler = (sender, args) =>
                {
                    imageSurface.LoadCompleted -= handler;
                    switch (args.Status)
                    {
                    case LoadedImageSourceLoadStatus.Success:
                        tcs.SetResult(sender);
                        break;

                    default:
                        tcs.SetException(new ImageSurfaceFailedStatusException(args.Status));
                        break;
                    }
                };

                imageSurface.LoadCompleted += handler;
                context.Current             = await tcs.Task;
            }

            await next(context, cancellationToken);
        }
        public static LoadedImageSurface GetVectorSurface(IProtoService protoService, File file, Color foreground)
        {
            if (file.Local.IsDownloadingCompleted)
            {
                var text = GetSvgXml(file);

                var bitmap = default(LoadedImageSurface);
                using (var stream = new InMemoryRandomAccessStream())
                {
                    try
                    {
                        var size = PlaceholderImageHelper.GetForCurrentView().DrawSvg(text, foreground, stream);
                        bitmap = LoadedImageSurface.StartLoadFromStream(stream, new Windows.Foundation.Size(size.Width / 3, size.Height / 3));
                    }
                    catch { }
                }

                return(bitmap);
            }
            else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive && protoService != null)
            {
                protoService.DownloadFile(file.Id, 1);
            }

            return(null);
        }
Beispiel #3
0
 public void LoadImageFromStream(IRandomAccessStream stream)
 {
     if (stream != null && IsImageLoading == false)
     {
         var compositor = Window.Current.Compositor;
         IsImageLoading          = true;
         _surface                = LoadedImageSurface.StartLoadFromStream(stream);
         _surface.LoadCompleted += Load_Completed;
     }
 }
Beispiel #4
0
        public static async Task <LoadedImageSurface> GetPatternSurfaceAsync(IProtoService protoService, File file)
        {
            using var locked = await _patternSurfaceLock.WaitAsync();

            if (file.Local.IsFileExisting())
            {
                var bitmap = default(LoadedImageSurface);

                var cache    = $"{file.Remote.UniqueId}.cache.png";
                var relative = System.IO.Path.Combine("wallpapers", cache);

                var item = await ApplicationData.Current.LocalFolder.TryGetItemAsync(relative) as StorageFile;

                if (item == null)
                {
                    item = await ApplicationData.Current.LocalFolder.CreateFileAsync(relative, CreationCollisionOption.ReplaceExisting);

                    using (var stream = await item.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        try
                        {
                            var text = await GetSvgXml(file);

                            await PlaceholderImageHelper.Current.DrawSvgAsync(text, Colors.White, stream);
                        }
                        catch { }
                    }
                }

                if (item != null)
                {
                    using (var stream = await item.OpenReadAsync())
                    {
                        try
                        {
                            var props = await item.Properties.GetImagePropertiesAsync();

                            bitmap = LoadedImageSurface.StartLoadFromStream(stream, new Size(props.Width / 2d, props.Height / 2d));
                        }
                        catch { }
                    }
                }

                return(bitmap);
            }
            else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive && protoService != null)
            {
                protoService.DownloadFile(file.Id, 1);
            }

            return(null);
        }
Beispiel #5
0
        public static LayerTranslator CreateImageLayerTranslator(ImageLayerContext context)
        {
            if (!Transforms.TryCreateContainerVisualTransformChain(context, out var containerVisualRootNode, out var containerVisualContentNode))
            {
                // The layer is never visible.
                return(null);
            }

            var imageAsset = GetImageAsset(context);

            if (imageAsset is null)
            {
                return(null);
            }

            var content = context.ObjectFactory.CreateSpriteVisual();

            containerVisualContentNode.Children.Add(content);
            content.Size = new Sn.Vector2((float)imageAsset.Width, (float)imageAsset.Height);

            LoadedImageSurface surface;
            var imageSize = $"{imageAsset.Width}x{imageAsset.Height}";

            switch (imageAsset.ImageType)
            {
            case ImageAsset.ImageAssetType.Embedded:
                var embeddedImageAsset = (EmbeddedImageAsset)imageAsset;
                surface = LoadedImageSurface.StartLoadFromStream(embeddedImageAsset.Bytes);
                surface.SetName(imageAsset.Id);
                surface.SetDescription(context, $"Image: \"{embeddedImageAsset.Id}\" {embeddedImageAsset.Format} {imageSize}.");
                break;

            case ImageAsset.ImageAssetType.External:
                var externalImageAsset = (ExternalImageAsset)imageAsset;
                surface = LoadedImageSurface.StartLoadFromUri(new Uri($"file://localhost/{externalImageAsset.Path}{externalImageAsset.FileName}"));
                surface.SetName(externalImageAsset.FileName);
                var path = externalImageAsset.Path + externalImageAsset.FileName;
                surface.SetDescription(context, $"\"{path}\" {imageSize}.");
                context.Issues.ImageFileRequired(path);
                break;

            default:
                throw new InvalidOperationException();
            }

            var imageBrush = context.ObjectFactory.CreateSurfaceBrush(surface);

            content.Brush = imageBrush;

            return(new LayerTranslator.FromVisual(containerVisualRootNode));
        }
Beispiel #6
0
        // This is only get called by ItemDetailsPage.Episodesitem_ContainerContentChanging
        public async void LoadImage()
        {
            ThumbnailCompositor = ElementCompositionPreview.GetElementVisual(Thumbnail).Compositor;

            using var stream = await GetImage();

            ThumbnailSurface = LoadedImageSurface.StartLoadFromStream(stream);
            ThumbnailSurface.LoadCompleted += ImgSurface_LoadCompleted;

            ThumbnailSurfaceBrush = ThumbnailCompositor.CreateSurfaceBrush(ThumbnailSurface);
            ThumbnailSurfaceBrush.BitmapInterpolationMode = CompositionBitmapInterpolationMode.Linear;
            ThumbnailSurfaceBrush.Stretch = CompositionStretch.UniformToFill;

            ThumbnailSpriteVisual         = ThumbnailCompositor.CreateSpriteVisual();
            ThumbnailSpriteVisual.Brush   = ThumbnailSurfaceBrush;
            ThumbnailSpriteVisual.Opacity = 0;
            ThumbnailSpriteVisual.Size    = new Vector2(180, 80);

            ElementCompositionPreview.SetElementChildVisual(Thumbnail, ThumbnailSpriteVisual);
        }
        internal override ICompositionSurface?LoadImage(Uri imageUri)
        {
            if (!imageUri.IsAbsoluteUri || imageUri.Authority != "localhost")
            {
                return(null);
            }

            // Load the image from the .lottie file. This is loaded into a MemoryStream
            // because the streams that come from ZipArchive cannot be randomly accessed
            // as required by LoadedImageSurface. This also has the benefit that it is
            // safe to Dispose the DotLottieFile as soon as the last image has started
            // decoding, becuase we already have all the bytes in the MemoryStream.
            var imageStream = _dotLottieFile !.OpenFileAsMemoryStream(imageUri.AbsolutePath);

            if (imageStream is null)
            {
                return(null);
            }

            return(LoadedImageSurface.StartLoadFromStream(imageStream.AsRandomAccessStream()));
        }
Beispiel #8
0
        private void RenderPicker(int w = 0, int h = 0)
        {
            if (_container == null)
            {
                return;
            }

            double width  = w > 0 ? w : this.Width;
            double height = h > 0 ? h : this.Height;

            if (width == -1 || height == -1)
            {
                return;
            }

            double colorVal = this.ColorValue;
            double offset   = this.HueOffset;
            bool   invert   = this.InvertSaturation;
            float  esize    = this.ElementSize;

            ColorPickerMode mode = this.Mode;

            _ = Task.Run(() =>
            {
                ColorPickerRenderer cw;

                if (w <= 0)
                {
                    if (double.IsNaN(width))
                    {
                        return;
                    }
                    w = (int)width;
                }
                if (h <= 0)
                {
                    if (double.IsNaN(height))
                    {
                        return;
                    }
                    h = (int)height;
                }

                if (w < 32 || h < 32)
                {
                    return;
                }

                if (mode == ColorPickerMode.Wheel || mode == ColorPickerMode.HexagonWheel)
                {
                    int rad;

                    if (h < w)
                    {
                        rad = h / 2;
                        w   = h;
                    }
                    else
                    {
                        rad = w / 2;
                        h   = w;
                    }

                    cw = new ColorPickerRenderer(rad, colorVal, offset, invert, true);
                }
                else
                {
                    cw = new ColorPickerRenderer(w, h, colorVal, offset, invert, mode == ColorPickerMode.LinearVertical, true);
                }

                DispatcherQueue.TryEnqueue(async() =>
                {
                    SKImage img;
                    SKBitmap bmp = new SKBitmap((int)cw.Bounds.Width, (int)cw.Bounds.Height, SKColorType.Bgra8888, SKAlphaType.Premul);

                    var ptr = bmp.GetPixels();

                    unsafe
                    {
                        var gch = GCHandle.Alloc(cw.ImageBytes, GCHandleType.Pinned);

                        Buffer.MemoryCopy((void *)gch.AddrOfPinnedObject(), (void *)ptr, cw.ImageBytes.Length, cw.ImageBytes.Length);
                        gch.Free();
                    }

                    bmp.SetImmutable();
                    img = SKImage.FromBitmap(bmp);

                    SKData encoded = img.Encode();
                    Stream stream  = encoded.AsStream();

                    //var ret = ImageSource.FromStream(() => stream);

                    cpRender = cw;


                    Compositor _compositor = _container.Compositor;
                    SpriteVisual _imageVisual;
                    CompositionSurfaceBrush _imageBrush;

                    _imageBrush = _compositor.CreateSurfaceBrush();

                    // The loadedSurface has a size of 0x0 till the image has been been downloaded, decoded and loaded to the surface. We can assign the surface to the CompositionSurfaceBrush and it will show up once the image is loaded to the surface.
                    LoadedImageSurface _loadedSurface = LoadedImageSurface.StartLoadFromStream(stream.AsRandomAccessStream());

                    _imageBrush.Surface = _loadedSurface;

                    _imageVisual       = _compositor.CreateSpriteVisual();
                    _imageVisual.Brush = _imageBrush;
                    _imageVisual.Size  = new Vector2(cw.Bounds.Width, cw.Bounds.Height);
                    //_imageVisual.Offset = new Vector3((float)Padding.Left, (float)Padding.Top, 0);

                    _container.Children.RemoveAll();
                    _container.Children.InsertAtBottom(_imageVisual);

                    currentShape  = null;
                    currentSprite = null;
                    currentGeo    = null;
                });
            });
        }