committed by
GitHub
30 changed files with 270 additions and 120 deletions
@ -1,45 +1,52 @@ |
|||
<UserControl xmlns="https://github.com/avaloniaui" |
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|||
x:Class="ControlCatalog.Pages.ImagePage"> |
|||
<StackPanel Orientation="Vertical" Spacing="4"> |
|||
<TextBlock Classes="h1">Image</TextBlock> |
|||
<TextBlock Classes="h2">Displays an image</TextBlock> |
|||
|
|||
<StackPanel Orientation="Horizontal" |
|||
Margin="0,16,0,0" |
|||
HorizontalAlignment="Center" |
|||
Spacing="16"> |
|||
<StackPanel Orientation="Vertical"> |
|||
<TextBlock>No Stretch</TextBlock> |
|||
<Image Source="/Assets/delicate-arch-896885_640.jpg" |
|||
Width="100" Height="200" |
|||
Stretch="None"/> |
|||
</StackPanel> |
|||
|
|||
<StackPanel Orientation="Vertical"> |
|||
<TextBlock>Fill</TextBlock> |
|||
<Image Source="/Assets/delicate-arch-896885_640.jpg" |
|||
Width="100" Height="200" |
|||
Stretch="Fill"/> |
|||
</StackPanel> |
|||
<DockPanel> |
|||
<StackPanel DockPanel.Dock="Top" Orientation="Vertical" Spacing="4"> |
|||
<TextBlock Classes="h1">Image</TextBlock> |
|||
<TextBlock Classes="h2">Displays an image</TextBlock> |
|||
</StackPanel> |
|||
|
|||
<StackPanel Orientation="Vertical"> |
|||
<TextBlock>Uniform</TextBlock> |
|||
<Image Source="/Assets/delicate-arch-896885_640.jpg" |
|||
Width="100" Height="200" |
|||
Stretch="Uniform"/> |
|||
</StackPanel> |
|||
<Grid ColumnDefinitions="*,*" RowDefinitions="Auto,*" Margin="64"> |
|||
|
|||
<DockPanel Grid.Column="0" Grid.Row="1" Margin="16"> |
|||
<TextBlock DockPanel.Dock="Top" Classes="h3" Margin="0 8">Bitmap</TextBlock> |
|||
<ComboBox Name="bitmapStretch" DockPanel.Dock="Top" SelectedIndex="2" SelectionChanged="BitmapStretchChanged"> |
|||
<ComboBoxItem>None</ComboBoxItem> |
|||
<ComboBoxItem>Fill</ComboBoxItem> |
|||
<ComboBoxItem>Uniform</ComboBoxItem> |
|||
<ComboBoxItem>UniformToFill</ComboBoxItem> |
|||
</ComboBox> |
|||
<Image Name="bitmapImage" |
|||
Source="/Assets/delicate-arch-896885_640.jpg"/> |
|||
</DockPanel> |
|||
|
|||
<StackPanel Orientation="Vertical"> |
|||
<TextBlock>UniformToFill</TextBlock> |
|||
<Image Source="/Assets/delicate-arch-896885_640.jpg" |
|||
Width="100" Height="200" |
|||
Stretch="UniformToFill"/> |
|||
</StackPanel> |
|||
</StackPanel> |
|||
<StackPanel Orientation="Vertical"> |
|||
<TextBlock>Window Icon as an Image</TextBlock> |
|||
<Image Name="Icon" Width="100" Height="200" Stretch="None" /> |
|||
</StackPanel> |
|||
</StackPanel> |
|||
<DockPanel Grid.Column="1" Grid.Row="1" Margin="16"> |
|||
<TextBlock DockPanel.Dock="Top" Classes="h3" Margin="0 8">Drawing</TextBlock> |
|||
<ComboBox Name="drawingStretch" DockPanel.Dock="Top" SelectedIndex="2" SelectionChanged="DrawingStretchChanged"> |
|||
<ComboBoxItem>None</ComboBoxItem> |
|||
<ComboBoxItem>Fill</ComboBoxItem> |
|||
<ComboBoxItem>Uniform</ComboBoxItem> |
|||
<ComboBoxItem>UniformToFill</ComboBoxItem> |
|||
</ComboBox> |
|||
<Image Name="drawingImage"> |
|||
<Image.Source> |
|||
<DrawingImage> |
|||
<GeometryDrawing Brush="Red"> |
|||
<PathGeometry> |
|||
<PathFigure StartPoint="0,0" IsClosed="True"> |
|||
<QuadraticBezierSegment Point1="50,0" Point2="50,-50" /> |
|||
<QuadraticBezierSegment Point1="100,-50" Point2="100,0" /> |
|||
<LineSegment Point="50,0" /> |
|||
<LineSegment Point="50,50" /> |
|||
</PathFigure> |
|||
</PathGeometry> |
|||
</GeometryDrawing> |
|||
</DrawingImage> |
|||
</Image.Source> |
|||
</Image> |
|||
</DockPanel> |
|||
</Grid> |
|||
|
|||
</DockPanel> |
|||
</UserControl> |
|||
|
|||
@ -0,0 +1,81 @@ |
|||
using System; |
|||
using Avalonia.Metadata; |
|||
using Avalonia.Platform; |
|||
using Avalonia.Visuals.Media.Imaging; |
|||
|
|||
namespace Avalonia.Media |
|||
{ |
|||
/// <summary>
|
|||
/// An <see cref="IImage"/> that uses a <see cref="Drawing"/> for content.
|
|||
/// </summary>
|
|||
public class DrawingImage : AvaloniaObject, IImage, IAffectsRender |
|||
{ |
|||
/// <summary>
|
|||
/// Defines the <see cref="Drawing"/> property.
|
|||
/// </summary>
|
|||
public static readonly StyledProperty<Drawing> DrawingProperty = |
|||
AvaloniaProperty.Register<DrawingImage, Drawing>(nameof(Drawing)); |
|||
|
|||
/// <inheritdoc/>
|
|||
public event EventHandler Invalidated; |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the drawing content.
|
|||
/// </summary>
|
|||
[Content] |
|||
public Drawing Drawing |
|||
{ |
|||
get => GetValue(DrawingProperty); |
|||
set => SetValue(DrawingProperty, value); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public Size Size => Drawing?.GetBounds().Size ?? default; |
|||
|
|||
/// <inheritdoc/>
|
|||
void IImage.Draw( |
|||
DrawingContext context, |
|||
Rect sourceRect, |
|||
Rect destRect, |
|||
BitmapInterpolationMode bitmapInterpolationMode) |
|||
{ |
|||
var drawing = Drawing; |
|||
|
|||
if (drawing == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var bounds = drawing.GetBounds(); |
|||
var scale = Matrix.CreateScale( |
|||
destRect.Width / sourceRect.Width, |
|||
destRect.Height / sourceRect.Height); |
|||
var translate = Matrix.CreateTranslation( |
|||
-sourceRect.X + destRect.X - bounds.X, |
|||
-sourceRect.Y + destRect.Y - bounds.Y); |
|||
|
|||
using (context.PushClip(destRect)) |
|||
using (context.PushPreTransform(translate * scale)) |
|||
{ |
|||
Drawing?.Draw(context); |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e) |
|||
{ |
|||
base.OnPropertyChanged(e); |
|||
|
|||
if (e.Property == DrawingProperty) |
|||
{ |
|||
RaiseInvalidated(EventArgs.Empty); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Raises the <see cref="Invalidated"/> event.
|
|||
/// </summary>
|
|||
/// <param name="e">The event args.</param>
|
|||
protected void RaiseInvalidated(EventArgs e) => Invalidated?.Invoke(this, e); |
|||
} |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
using Avalonia.Platform; |
|||
using Avalonia.Visuals.Media.Imaging; |
|||
|
|||
namespace Avalonia.Media |
|||
{ |
|||
/// <summary>
|
|||
/// Represents a raster or vector image.
|
|||
/// </summary>
|
|||
public interface IImage |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the size of the image, in device independent pixels.
|
|||
/// </summary>
|
|||
Size Size { get; } |
|||
|
|||
/// <summary>
|
|||
/// Draws the image to a <see cref="DrawingContext"/>.
|
|||
/// </summary>
|
|||
/// <param name="context">The drawing context.</param>
|
|||
/// <param name="sourceRect">The rect in the image to draw.</param>
|
|||
/// <param name="destRect">The rect in the output to draw to.</param>
|
|||
/// <param name="bitmapInterpolationMode">The bitmap interpolation mode.</param>
|
|||
void Draw( |
|||
DrawingContext context, |
|||
Rect sourceRect, |
|||
Rect destRect, |
|||
BitmapInterpolationMode bitmapInterpolationMode); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue