ImagePage.xaml.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Media;
  4. using Avalonia.Media.Imaging;
  5. namespace ControlCatalog.Pages
  6. {
  7. public partial class ImagePage : UserControl
  8. {
  9. public ImagePage()
  10. {
  11. InitializeComponent();
  12. }
  13. public void BitmapStretchChanged(object sender, SelectionChangedEventArgs e)
  14. {
  15. if (bitmapImage != null)
  16. {
  17. var comboxBox = (ComboBox)sender;
  18. bitmapImage.Stretch = (Stretch)comboxBox.SelectedIndex;
  19. }
  20. }
  21. public void DrawingStretchChanged(object sender, SelectionChangedEventArgs e)
  22. {
  23. if (drawingImage != null)
  24. {
  25. var comboxBox = (ComboBox)sender;
  26. drawingImage.Stretch = (Stretch)comboxBox.SelectedIndex;
  27. }
  28. }
  29. public void BitmapCropChanged(object sender, SelectionChangedEventArgs e)
  30. {
  31. if (croppedImage != null)
  32. {
  33. var comboxBox = (ComboBox)sender;
  34. if (croppedImage.Source is CroppedBitmap croppedBitmap)
  35. {
  36. croppedBitmap.SourceRect = GetCropRect(comboxBox.SelectedIndex);
  37. }
  38. }
  39. }
  40. private static PixelRect GetCropRect(int index)
  41. {
  42. var bitmapWidth = 640;
  43. var bitmapHeight = 426;
  44. var cropSize = new PixelSize(320, 240);
  45. return index switch
  46. {
  47. 1 => new PixelRect(new PixelPoint((bitmapWidth - cropSize.Width) / 2, (bitmapHeight - cropSize.Width) / 2), cropSize),
  48. 2 => new PixelRect(new PixelPoint(0, 0), cropSize),
  49. 3 => new PixelRect(new PixelPoint(bitmapWidth - cropSize.Width, 0), cropSize),
  50. 4 => new PixelRect(new PixelPoint(0, bitmapHeight - cropSize.Height), cropSize),
  51. 5 => new PixelRect(new PixelPoint(bitmapWidth - cropSize.Width, bitmapHeight - cropSize.Height), cropSize),
  52. _ => default
  53. };
  54. }
  55. }
  56. }