PostImageViewModel.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Avalonia.Media.Imaging;
  2. using ReactiveUI;
  3. using System;
  4. using System.Threading.Tasks;
  5. using VeloeAvaloniaKemonoPartyApp.Models;
  6. namespace VeloeAvaloniaKemonoPartyApp.ViewModels
  7. {
  8. public class PostImageViewModel : ViewModelBase
  9. {
  10. private Attachment _attachment;
  11. public PostImageViewModel(Attachment attachment)
  12. {
  13. _attachment = attachment;
  14. }
  15. private Bitmap? _cover;
  16. public Bitmap? Cover
  17. {
  18. get => _cover;
  19. private set => this.RaiseAndSetIfChanged(ref _cover, value);
  20. }
  21. public async Task LoadAvatar()
  22. {
  23. await using (var imageStream = await _attachment.LoadCoverBitmapAsync())
  24. {
  25. if (imageStream is null) return;
  26. Cover = await Task.Run(() =>
  27. {
  28. Bitmap? bitmap;
  29. try
  30. {
  31. bitmap = new Bitmap(imageStream);
  32. bitmap = bitmap.CreateScaledBitmap(new Avalonia.PixelSize(bitmap.PixelSize.Width / 2, bitmap.PixelSize.Height / 2));
  33. }
  34. catch (Exception ex)
  35. {
  36. bitmap = null;
  37. }
  38. return bitmap;
  39. });
  40. }
  41. }
  42. }
  43. }