PostImageViewModel.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Avalonia.Interactivity;
  2. using Avalonia.Media.Imaging;
  3. using Avalonia.Threading;
  4. using HanumanInstitute.MvvmDialogs;
  5. using ReactiveUI;
  6. using System;
  7. using System.Threading.Tasks;
  8. using VeloeAvaloniaKemonoPartyApp.Models;
  9. namespace VeloeAvaloniaKemonoPartyApp.ViewModels
  10. {
  11. public class PostImageViewModel : ViewModelBase
  12. {
  13. private Attachment _attachment;
  14. private readonly IDialogService _dialogService;
  15. public PostImageViewModel(IDialogService dialogService,Attachment attachment)
  16. {
  17. _attachment = attachment;
  18. _dialogService = dialogService;
  19. }
  20. private Bitmap? _cover;
  21. public Bitmap? Cover
  22. {
  23. get => _cover;
  24. private set => this.RaiseAndSetIfChanged(ref _cover, value);
  25. }
  26. public async Task LoadAvatar()
  27. {
  28. await using (var imageStream = await _attachment.LoadCoverBitmapAsync())
  29. {
  30. if (imageStream is null) return;
  31. Cover = await Task.Run(() =>
  32. {
  33. Bitmap? bitmap;
  34. try
  35. {
  36. bitmap = new Bitmap(imageStream);
  37. bitmap = bitmap.CreateScaledBitmap(new Avalonia.PixelSize(bitmap.PixelSize.Width / 2, bitmap.PixelSize.Height / 2));
  38. }
  39. catch (Exception ex)
  40. {
  41. bitmap = null;
  42. }
  43. return bitmap;
  44. });
  45. }
  46. }
  47. public async Task OnImageClick(object sender, RoutedEventArgs e)
  48. {
  49. var vm = new ImageZoomViewModel(_attachment);
  50. _dialogService.Show((_dialogService.DialogManager as HanumanInstitute.MvvmDialogs.Avalonia.DialogManager).NavigationManager.CurrentViewModel, vm);
  51. }
  52. }
  53. }