public static void UpdateIsAnimationPlaying(this WImage imageView, IImageSourcePart image) { if (!IsAnimationSupported) { return; } if (imageView.Source is BitmapImage bitmapImage && bitmapImage.IsAnimatedBitmap) { if (image.IsAnimationPlaying) { if (!bitmapImage.IsPlaying) { bitmapImage.Play(); } } else { if (bitmapImage.IsPlaying) { bitmapImage.Stop(); } } } }
public static void BindSoftwareBitmapToImageControl(Microsoft.UI.Xaml.Controls.Image imageControl, SoftwareBitmap softwareBitmap) { SoftwareBitmap displayBitmap = softwareBitmap; //Image control only accepts BGRA8 encoding and Premultiplied/no alpha channel. This checks and converts //the SoftwareBitmap we want to bind. if (displayBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || displayBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied) { displayBitmap = SoftwareBitmap.Convert(displayBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } // get software bitmap souce var source = new SoftwareBitmapSource(); source.SetBitmapAsync(displayBitmap).GetAwaiter(); // draw the input image imageControl.Source = source; }
protected async override void OnElementChanged(ElementChangedEventArgs <ImageButton> e) { base.OnElementChanged(e); if (e.NewElement != null) { if (Control == null) { _formsButton = new FormsButton(); _formsButton.Padding = WinUIHelpers.CreateThickness(0); _formsButton.BorderThickness = WinUIHelpers.CreateThickness(0); _formsButton.Background = null; _image = new Microsoft.UI.Xaml.Controls.Image() { VerticalAlignment = Microsoft.UI.Xaml.VerticalAlignment.Center, HorizontalAlignment = Microsoft.UI.Xaml.HorizontalAlignment.Center, Stretch = WStretch.Uniform, }; _image.ImageOpened += OnImageOpened; _image.ImageFailed += OnImageFailed; _formsButton.Content = _image; _formsButton.Click += OnButtonClick; _formsButton.AddHandler(PointerPressedEvent, new PointerEventHandler(OnPointerPressed), true); _formsButton.Loaded += ButtonOnLoaded; SetNativeControl(_formsButton); } else { WireUpFormsVsm(); } //TODO: We may want to revisit this strategy later. If a user wants to reset any of these to the default, the UI won't update. if (Element.IsSet(VisualElement.BackgroundColorProperty) && Element.BackgroundColor != (Color)VisualElement.BackgroundColorProperty.DefaultValue) { UpdateImageButtonBackground(); } if (Element.IsSet(VisualElement.BackgroundProperty) && Element.Background != (Brush)VisualElement.BackgroundProperty.DefaultValue) { UpdateImageButtonBackground(); } if (Element.IsSet(ImageButton.BorderColorProperty) && Element.BorderColor != (Color)ImageButton.BorderColorProperty.DefaultValue) { UpdateBorderColor(); } if (Element.IsSet(ImageButton.BorderWidthProperty) && Element.BorderWidth != (double)ImageButton.BorderWidthProperty.DefaultValue) { UpdateBorderWidth(); } if (Element.IsSet(ImageButton.CornerRadiusProperty) && Element.CornerRadius != (int)ImageButton.CornerRadiusProperty.DefaultValue) { UpdateBorderRadius(); } // By default Button loads width padding 8, 4, 8 ,4 if (Element.IsSet(Button.PaddingProperty)) { UpdatePadding(); } await TryUpdateSource().ConfigureAwait(false); } }
public static void Clear(this WImage imageView) { imageView.Source = null; }
public static void UpdateAspect(this WImage imageView, IImage image) { imageView.Stretch = image.Aspect.ToStretch(); }
public static async Task <IImageSourceServiceResult <WImageSource>?> UpdateSourceAsync(this WImage imageView, IImageSourcePart image, IImageSourceServiceProvider services, CancellationToken cancellationToken = default) { imageView.Clear(); image.UpdateIsLoading(false); var imageSource = image.Source; if (imageSource == null) { return(null); } var events = image as IImageSourcePartEvents; events?.LoadingStarted(); image.UpdateIsLoading(true); try { var service = services.GetRequiredImageSourceService(imageSource); var scale = imageView.XamlRoot?.RasterizationScale ?? 1; var result = await service.GetImageSourceAsync(imageSource, (float)scale, cancellationToken); var uiImage = result?.Value; var applied = !cancellationToken.IsCancellationRequested && imageSource == image.Source; // only set the image if we are still on the same one if (applied) { imageView.Source = uiImage; imageView.UpdateIsAnimationPlaying(image); } events?.LoadingCompleted(applied); return(result); } catch (OperationCanceledException) { // no-op events?.LoadingCompleted(false); } catch (Exception ex) { events?.LoadingFailed(ex); } finally { // only mark as finished if we are still working on the same image if (imageSource == image.Source) { image.UpdateIsLoading(false); } } return(null); }