Browse Source

Add class CroppedBitmap

pull/3589/head
Вадим Мельников 6 years ago
parent
commit
c34bfc56f8
  1. 15
      samples/ControlCatalog/Pages/ImagePage.xaml
  2. 41
      samples/ControlCatalog/Pages/ImagePage.xaml.cs
  3. 47
      src/Avalonia.Visuals/Media/Imaging/CroppedBitmap.cs

15
samples/ControlCatalog/Pages/ImagePage.xaml

@ -7,7 +7,7 @@
<TextBlock Classes="h2">Displays an image</TextBlock>
</StackPanel>
<Grid ColumnDefinitions="*,*" RowDefinitions="Auto,*" Margin="64">
<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>
@ -22,6 +22,19 @@
</DockPanel>
<DockPanel Grid.Column="1" Grid.Row="1" Margin="16">
<TextBlock DockPanel.Dock="Top" Classes="h3" Margin="0 8">Crop</TextBlock>
<ComboBox Name="bitmapCrop" DockPanel.Dock="Top" SelectedIndex="0" SelectionChanged="BitmapCropChanged">
<ComboBoxItem>None</ComboBoxItem>
<ComboBoxItem>Center</ComboBoxItem>
<ComboBoxItem>TopLeft</ComboBoxItem>
<ComboBoxItem>TopRight</ComboBoxItem>
<ComboBoxItem>BottomLeft</ComboBoxItem>
<ComboBoxItem>BottomRight</ComboBoxItem>
</ComboBox>
<Image Name="croppedImage"/>
</DockPanel>
<DockPanel Grid.Column="2" 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>

41
samples/ControlCatalog/Pages/ImagePage.xaml.cs

@ -1,6 +1,10 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
namespace ControlCatalog.Pages
{
@ -8,12 +12,17 @@ namespace ControlCatalog.Pages
{
private readonly Image _bitmapImage;
private readonly Image _drawingImage;
private readonly Image _croppedImage;
private readonly IBitmap _croppedBitmapSource;
public ImagePage()
{
InitializeComponent();
_bitmapImage = this.FindControl<Image>("bitmapImage");
_drawingImage = this.FindControl<Image>("drawingImage");
_croppedImage = this.FindControl<Image>("croppedImage");
_croppedBitmapSource = LoadBitmap("avares://ControlCatalog/Assets/delicate-arch-896885_640.jpg");
_croppedImage.Source = new CroppedBitmap(_croppedBitmapSource, default);
}
private void InitializeComponent()
@ -38,5 +47,37 @@ namespace ControlCatalog.Pages
_drawingImage.Stretch = (Stretch)comboxBox.SelectedIndex;
}
}
public void BitmapCropChanged(object sender, SelectionChangedEventArgs e)
{
if (_croppedImage != null)
{
var comboxBox = (ComboBox)sender;
_croppedImage.Source = new CroppedBitmap( _croppedBitmapSource, GetCropRect(comboxBox.SelectedIndex));
}
}
private PixelRect GetCropRect(int index)
{
var bitmapWidth = _croppedBitmapSource.PixelSize.Width;
var bitmapHeight = _croppedBitmapSource.PixelSize.Height;
var cropSize = new PixelSize(bitmapWidth / 2, bitmapHeight / 2);
return index switch
{
1 => new PixelRect(new PixelPoint((bitmapWidth - cropSize.Width) / 2, (bitmapHeight - cropSize.Width) / 2), cropSize),
2 => new PixelRect(new PixelPoint(0, 0), cropSize),
3 => new PixelRect(new PixelPoint(bitmapWidth - cropSize.Width, 0), cropSize),
4 => new PixelRect(new PixelPoint(0, bitmapHeight - cropSize.Height), cropSize),
5 => new PixelRect(new PixelPoint(bitmapWidth - cropSize.Width, bitmapHeight - cropSize.Height), cropSize),
_ => PixelRect.Empty
};
}
private IBitmap LoadBitmap(string uri)
{
var assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
return new Bitmap(assets.Open(new Uri(uri)));
}
}
}

47
src/Avalonia.Visuals/Media/Imaging/CroppedBitmap.cs

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Text;
using Avalonia.Visuals.Media.Imaging;
namespace Avalonia.Media.Imaging
{
public class CroppedBitmap : IImage, IDisposable
{
public CroppedBitmap()
{
Source = null;
SourceRect = default;
}
public CroppedBitmap(IBitmap source, PixelRect sourceRect)
{
Source = source;
SourceRect = sourceRect;
}
public virtual void Dispose()
{
Source?.Dispose();
}
public Size Size {
get
{
if (Source == null)
return Size.Empty;
if (SourceRect.IsEmpty)
return Source.Size;
return SourceRect.Size.ToSizeWithDpi(Source.Dpi);
}
}
public void Draw(DrawingContext context, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode)
{
if (Source == null)
return;
var topLeft = SourceRect.TopLeft.ToPointWithDpi(Source.Dpi);
Source.Draw(context, sourceRect.Translate(new Vector(topLeft.X, topLeft.Y)), destRect, bitmapInterpolationMode);
}
public IBitmap Source { get; }
public PixelRect SourceRect { get; }
}
}
Loading…
Cancel
Save