ImagePage.xaml.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Markup.Xaml;
  5. using Avalonia.Media;
  6. using Avalonia.Media.Imaging;
  7. using Avalonia.Platform;
  8. namespace ControlCatalog.Pages
  9. {
  10. public class ImagePage : UserControl
  11. {
  12. private readonly Image _bitmapImage;
  13. private readonly Image _drawingImage;
  14. private readonly Image _croppedImage;
  15. public ImagePage()
  16. {
  17. InitializeComponent();
  18. _bitmapImage = this.Get<Image>("bitmapImage");
  19. _drawingImage = this.Get<Image>("drawingImage");
  20. _croppedImage = this.Get<Image>("croppedImage");
  21. }
  22. private void InitializeComponent()
  23. {
  24. AvaloniaXamlLoader.Load(this);
  25. }
  26. public void BitmapStretchChanged(object sender, SelectionChangedEventArgs e)
  27. {
  28. if (_bitmapImage != null)
  29. {
  30. var comboxBox = (ComboBox)sender;
  31. _bitmapImage.Stretch = (Stretch)comboxBox.SelectedIndex;
  32. }
  33. }
  34. public void DrawingStretchChanged(object sender, SelectionChangedEventArgs e)
  35. {
  36. if (_drawingImage != null)
  37. {
  38. var comboxBox = (ComboBox)sender;
  39. _drawingImage.Stretch = (Stretch)comboxBox.SelectedIndex;
  40. }
  41. }
  42. public void BitmapCropChanged(object sender, SelectionChangedEventArgs e)
  43. {
  44. if (_croppedImage != null)
  45. {
  46. var comboxBox = (ComboBox)sender;
  47. if (_croppedImage.Source is CroppedBitmap croppedBitmap)
  48. {
  49. croppedBitmap.SourceRect = GetCropRect(comboxBox.SelectedIndex);
  50. }
  51. }
  52. }
  53. private static PixelRect GetCropRect(int index)
  54. {
  55. var bitmapWidth = 640;
  56. var bitmapHeight = 426;
  57. var cropSize = new PixelSize(320, 240);
  58. return index switch
  59. {
  60. 1 => new PixelRect(new PixelPoint((bitmapWidth - cropSize.Width) / 2, (bitmapHeight - cropSize.Width) / 2), cropSize),
  61. 2 => new PixelRect(new PixelPoint(0, 0), cropSize),
  62. 3 => new PixelRect(new PixelPoint(bitmapWidth - cropSize.Width, 0), cropSize),
  63. 4 => new PixelRect(new PixelPoint(0, bitmapHeight - cropSize.Height), cropSize),
  64. 5 => new PixelRect(new PixelPoint(bitmapWidth - cropSize.Width, bitmapHeight - cropSize.Height), cropSize),
  65. _ => PixelRect.Empty
  66. };
  67. }
  68. }
  69. }