public void OpenImages() { LogPanel.Log("Opening images..."); var dialog = new OpenFileDialog { Multiselect = true }; if (dialog.ShowDialog() == true) { var ans = dialog.FileNames; if (ans != null) { images = new List <MyImage>(); foreach (var f in ans) { LogPanel.Log(f); images.Add(new MyImage(f)); } LogPanel.Log($"{ans.Length} files selected."); ImageOpened?.Invoke(); ShowImages(); } else { LogPanel.Log("No files selected."); } } else { LogPanel.Log("Canceled."); } }
private void OnSourceChanged(ImageSource newValue, ImageSource oldValue) { if (newValue is WriteableBitmap bitmap && bitmap.PixelWidth > 0 && bitmap.PixelHeight > 0) { ImageOpened?.Invoke(this, null); } }
#pragma warning restore IDE1006 // Naming Styles protected override void OnApplyTemplate() { part_imageForeground = (Image)GetTemplateChild(nameof(part_imageForeground)); part_imageForeground.ImageOpened += (s, e) => ImageOpened?.Invoke(this, new RoutedEventArgs()); part_imageBackground = (Image)GetTemplateChild(nameof(part_imageBackground)); part_imageBackground.ImageOpened += (s, e) => part_imageForeground.Source = part_imageBackground.Source; part_imageBackground.ImageFailed += (s, e) => ImageFailed?.Invoke(this, new RoutedEventArgs()); }
private void OnImageOpened() { if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug)) { this.Log().Debug(ToString() + " Image opened successfully"); } ImageOpened?.Invoke(this, RoutedEventArgs.Empty); }
protected virtual void OnImageOpened(ImageSource imageSource) { if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug)) { this.Log().Debug(this.ToString() + " Image opened successfully"); } ImageOpened?.Invoke(this, RoutedEventArgs.Empty); _successfullyOpenedImage = imageSource; }
protected override async void OnApplyTemplate() { base.OnApplyTemplate(); PART_Image = GetTemplateChild(nameof(PART_Image)) as Image; PART_Image.ImageOpened += (object sender, RoutedEventArgs e) => ImageOpened?.Invoke(sender, e); PART_Image.ImageFailed += (object sender, ExceptionRoutedEventArgs e) => ImageFailed?.Invoke(sender, e); await SetImageSource(UriSource); }
/// <summary> /// Bitmap Opened /// </summary> /// <param name="source"></param> private void BitmapOpened(BitmapSource source) { this.PixelWidth = source.PixelWidth; this.PixelHeight = source.PixelHeight; var pixelPanel = this.ItemsPanelRoot as PixelPanel; if (pixelPanel != null) { pixelPanel.SetPixelSize(source.PixelWidth, source.PixelHeight); } ImageOpened?.Invoke(this, new RoutedEventArgs()); }
private async void SetSource(string source) { if (_image != null) { // 设计模式下直接显示。 if (DesignMode.DesignModeEnabled) { _image.Source = source == null ? null : new BitmapImage(new Uri(source, UriKind.RelativeOrAbsolute)); } else { if (source == null) { _image.Source = null; VisualStateManager.GoToState(this, NormalStateName, true); } else { VisualStateManager.GoToState(this, LoadingStateName, true); var result = await Loader.GetBitmapAsync(source); if (source == Source)// 确保在执行异步操作过程中,Source 没有变动。 { switch (result.Status) { case BitmapStatus.Opened: _image.Source = result.Value; VisualStateManager.GoToState(this, OpenedStateName, true); ImageOpened?.Invoke(this, EventArgs.Empty); break; case BitmapStatus.Failed: _image.Source = null; VisualStateManager.GoToState(this, FailedStateName, true); ImageFailed?.Invoke(this, new ImageFailedEventArgs(source, result.FailedException)); break; default: throw new ArgumentOutOfRangeException(nameof(result.Status)); } } } } } }
private async void SetSource(string source) { var imageBrush = AssociatedObject; if (imageBrush != null) { // 设计模式下直接显示。 if ((bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue) { imageBrush.ImageSource = source == null ? null : new BitmapImage(new Uri(source, UriKind.RelativeOrAbsolute)); } else { if (source == null) { imageBrush.ImageSource = null; } else { var result = await Loader.GetBitmapAsync(source); if (source == Source) // 确保在执行异步操作过程中,Source 没有变动。 { switch (result.Status) { case BitmapStatus.Opened: imageBrush.ImageSource = result.Value; ImageOpened?.Invoke(this, EventArgs.Empty); break; case BitmapStatus.Failed: imageBrush.ImageSource = null; ImageFailed?.Invoke(this, new ImageFailedEventArgs(source, result.FailedException)); break; default: throw new ArgumentOutOfRangeException(nameof(result.Status)); } } } } } }
private void ImageControlOnImageOpened(object sender, RoutedEventArgs routedEventArgs) { ImageOpened?.Invoke(this, routedEventArgs); var visual = ElementCompositionPreview.GetElementVisual((UIElement)sender); visual.Scale = new Vector3(0.9f, 0.9f, 1); visual.Opacity = 0; visual.CenterPoint = new Vector3((float)ImageControl.ActualWidth / 2, (float)ImageControl.ActualHeight / 2, 0); var visualImplicitAnimations = _compositor.CreateImplicitAnimationCollection(); var animation = _compositor.CreateVector3KeyFrameAnimation(); animation.Target = nameof(Visual.Scale); animation.InsertExpressionKeyFrame(1.0f, "this.FinalValue"); animation.Duration = TimeSpan.FromMilliseconds(AnimationDuration); visualImplicitAnimations[nameof(Visual.Scale)] = animation; visual.ImplicitAnimations = visualImplicitAnimations; var insetClip = _compositor.CreateInsetClip(0, 0, 0, 0); visual.Clip = insetClip; var opacityAnimation = _compositor.CreateScalarKeyFrameAnimation( nameof(Visual.Opacity), 0.0f, 1.0f, TimeSpan.FromMilliseconds(500)); visual.StartAnimation(nameof(Visual.Opacity), opacityAnimation); visual.Scale = new Vector3(1, 1, 1); var detailVisual = ElementCompositionPreview.GetElementVisual(DetailPanel); detailVisual.Opacity = 0; var finalOffset = new Vector3(0, (float)RootVisual.ActualHeight, 0); detailVisual.Offset = finalOffset; _isLoaded = true; }
private void Holder_ImageOpened(object sender, RoutedEventArgs e) { ImageOpened?.Invoke(this, e); }
private void OnImageOpened(object sender, RoutedEventArgs e) { ImageOpened?.Invoke(this, e); ImageExOpened?.Invoke(this, new ImageExOpenedEventArgs()); VisualStateManager.GoToState(this, LoadedState, true); }
private void Image_ImageOpened(object sender, RoutedEventArgs e) { ImageOpened?.Invoke(this, EventArgs.Empty); }
private async Task SetDesignSourceAsync(object?source) { if (_image == null) { return; } _lastLoadCts?.Cancel(); if (source == null) { AttachDesignSource(null); VisualStateManager.GoToState(this, EmptyStateName, true); return; } var loadCts = new CancellationTokenSource(); _lastLoadCts = loadCts; try { IsLoading = true; VisualStateManager.GoToState(this, LoadingStateName, true); // 开始 Loading,重置 DownloadProgress DownloadProgress = default; var context = new LoadingContext <ImageSource>(_uiContext, source, AttachDesignSource, ActualWidth, ActualHeight); context.DownloadProgressChanged += (sender, progress) => { if (_uiContext != null) { _uiContext.Post(state => { DownloadProgress = progress; }, null); } else { DownloadProgress = progress; } }; var pipeDelegate = ImageExService.GetHandler <ImageSource>(); var retryCount = RetryCount; var retryDelay = RetryDelay; var policy = Policy.Handle <Exception>() .WaitAndRetryAsync(retryCount, count => retryDelay, (ex, delay) => { context.Reset(); }); await policy.ExecuteAsync(() => pipeDelegate.Invoke(context, loadCts.Token)); if (!loadCts.IsCancellationRequested) { VisualStateManager.GoToState(this, OpenedStateName, true); if (_uiContext != null) { _uiContext.Post(state => { ImageOpened?.Invoke(this, EventArgs.Empty); }, null); } else { ImageOpened?.Invoke(this, EventArgs.Empty); } } } catch (Exception ex) { if (!loadCts.IsCancellationRequested) { AttachDesignSource(null); VisualStateManager.GoToState(this, FailedStateName, true); if (_uiContext != null) { _uiContext.Post(state => { ImageFailed?.Invoke(this, new ImageExFailedEventArgs(source, ex)); }, null); } else { ImageFailed?.Invoke(this, new ImageExFailedEventArgs(source, ex)); } } } finally { if (!loadCts.IsCancellationRequested) { IsLoading = false; } } }
private void RaiseImageOpened(RoutedEventArgs args) { ImageOpened?.Invoke(this, args); }