Image.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Avalonia.Media;
  5. using Avalonia.Media.Imaging;
  6. namespace Avalonia.Controls
  7. {
  8. /// <summary>
  9. /// Displays a <see cref="Bitmap"/> image.
  10. /// </summary>
  11. public class Image : Control
  12. {
  13. /// <summary>
  14. /// Defines the <see cref="Source"/> property.
  15. /// </summary>
  16. public static readonly StyledProperty<IBitmap> SourceProperty =
  17. AvaloniaProperty.Register<Image, IBitmap>(nameof(Source));
  18. /// <summary>
  19. /// Defines the <see cref="Stretch"/> property.
  20. /// </summary>
  21. public static readonly StyledProperty<Stretch> StretchProperty =
  22. AvaloniaProperty.Register<Image, Stretch>(nameof(Stretch), Stretch.Uniform);
  23. static Image()
  24. {
  25. AffectsRender(SourceProperty);
  26. AffectsRender(StretchProperty);
  27. }
  28. /// <summary>
  29. /// Gets or sets the bitmap image that will be displayed.
  30. /// </summary>
  31. public IBitmap Source
  32. {
  33. get { return GetValue(SourceProperty); }
  34. set { SetValue(SourceProperty, value); }
  35. }
  36. /// <summary>
  37. /// Gets or sets a value controlling how the image will be stretched.
  38. /// </summary>
  39. public Stretch Stretch
  40. {
  41. get { return (Stretch)GetValue(StretchProperty); }
  42. set { SetValue(StretchProperty, value); }
  43. }
  44. /// <summary>
  45. /// Renders the control.
  46. /// </summary>
  47. /// <param name="context">The drawing context.</param>
  48. public override void Render(DrawingContext context)
  49. {
  50. var source = Source;
  51. if (source != null)
  52. {
  53. Rect viewPort = new Rect(Bounds.Size);
  54. Size sourceSize = new Size(source.PixelWidth, source.PixelHeight);
  55. Vector scale = Stretch.CalculateScaling(Bounds.Size, sourceSize);
  56. Size scaledSize = sourceSize * scale;
  57. Rect destRect = viewPort
  58. .CenterRect(new Rect(scaledSize))
  59. .Intersect(viewPort);
  60. Rect sourceRect = new Rect(sourceSize)
  61. .CenterRect(new Rect(destRect.Size / scale));
  62. context.DrawImage(source, 1, sourceRect, destRect);
  63. }
  64. }
  65. /// <summary>
  66. /// Measures the control.
  67. /// </summary>
  68. /// <param name="availableSize">The available size.</param>
  69. /// <returns>The desired size of the control.</returns>
  70. protected override Size MeasureOverride(Size availableSize)
  71. {
  72. var source = Source;
  73. if (source != null)
  74. {
  75. Size sourceSize = new Size(source.PixelWidth, source.PixelHeight);
  76. if (double.IsInfinity(availableSize.Width) || double.IsInfinity(availableSize.Height))
  77. {
  78. return sourceSize;
  79. }
  80. else
  81. {
  82. return Stretch.CalculateSize(availableSize, sourceSize);
  83. }
  84. }
  85. else
  86. {
  87. return new Size();
  88. }
  89. }
  90. }
  91. }