ImagePage.xaml.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.FindControl<Image>("bitmapImage");
  19. _drawingImage = this.FindControl<Image>("drawingImage");
  20. _croppedImage = this.FindControl<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. var croppedBitmap = _croppedImage.Source as CroppedBitmap;
  48. croppedBitmap.SourceRect = GetCropRect(comboxBox.SelectedIndex);
  49. }
  50. }
  51. private PixelRect GetCropRect(int index)
  52. {
  53. var bitmapWidth = 640;
  54. var bitmapHeight = 426;
  55. var cropSize = new PixelSize(320, 240);
  56. return index switch
  57. {
  58. 1 => new PixelRect(new PixelPoint((bitmapWidth - cropSize.Width) / 2, (bitmapHeight - cropSize.Width) / 2), cropSize),
  59. 2 => new PixelRect(new PixelPoint(0, 0), cropSize),
  60. 3 => new PixelRect(new PixelPoint(bitmapWidth - cropSize.Width, 0), cropSize),
  61. 4 => new PixelRect(new PixelPoint(0, bitmapHeight - cropSize.Height), cropSize),
  62. 5 => new PixelRect(new PixelPoint(bitmapWidth - cropSize.Width, bitmapHeight - cropSize.Height), cropSize),
  63. _ => PixelRect.Empty
  64. };
  65. }
  66. }
  67. }