committed by
GitHub
30 changed files with 270 additions and 120 deletions
@ -1,45 +1,52 @@ |
|||||
<UserControl xmlns="https://github.com/avaloniaui" |
<UserControl xmlns="https://github.com/avaloniaui" |
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
x:Class="ControlCatalog.Pages.ImagePage"> |
x:Class="ControlCatalog.Pages.ImagePage"> |
||||
<StackPanel Orientation="Vertical" Spacing="4"> |
<DockPanel> |
||||
<TextBlock Classes="h1">Image</TextBlock> |
<StackPanel DockPanel.Dock="Top" Orientation="Vertical" Spacing="4"> |
||||
<TextBlock Classes="h2">Displays an image</TextBlock> |
<TextBlock Classes="h1">Image</TextBlock> |
||||
|
<TextBlock Classes="h2">Displays an image</TextBlock> |
||||
<StackPanel Orientation="Horizontal" |
</StackPanel> |
||||
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> |
|
||||
|
|
||||
<StackPanel Orientation="Vertical"> |
<Grid ColumnDefinitions="*,*" RowDefinitions="Auto,*" Margin="64"> |
||||
<TextBlock>Uniform</TextBlock> |
|
||||
<Image Source="/Assets/delicate-arch-896885_640.jpg" |
<DockPanel Grid.Column="0" Grid.Row="1" Margin="16"> |
||||
Width="100" Height="200" |
<TextBlock DockPanel.Dock="Top" Classes="h3" Margin="0 8">Bitmap</TextBlock> |
||||
Stretch="Uniform"/> |
<ComboBox Name="bitmapStretch" DockPanel.Dock="Top" SelectedIndex="2" SelectionChanged="BitmapStretchChanged"> |
||||
</StackPanel> |
<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"> |
<DockPanel Grid.Column="1" Grid.Row="1" Margin="16"> |
||||
<TextBlock>UniformToFill</TextBlock> |
<TextBlock DockPanel.Dock="Top" Classes="h3" Margin="0 8">Drawing</TextBlock> |
||||
<Image Source="/Assets/delicate-arch-896885_640.jpg" |
<ComboBox Name="drawingStretch" DockPanel.Dock="Top" SelectedIndex="2" SelectionChanged="DrawingStretchChanged"> |
||||
Width="100" Height="200" |
<ComboBoxItem>None</ComboBoxItem> |
||||
Stretch="UniformToFill"/> |
<ComboBoxItem>Fill</ComboBoxItem> |
||||
</StackPanel> |
<ComboBoxItem>Uniform</ComboBoxItem> |
||||
</StackPanel> |
<ComboBoxItem>UniformToFill</ComboBoxItem> |
||||
<StackPanel Orientation="Vertical"> |
</ComboBox> |
||||
<TextBlock>Window Icon as an Image</TextBlock> |
<Image Name="drawingImage"> |
||||
<Image Name="Icon" Width="100" Height="200" Stretch="None" /> |
<Image.Source> |
||||
</StackPanel> |
<DrawingImage> |
||||
</StackPanel> |
<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> |
</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