Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DrawingView ImageSize #1037

Merged
merged 2 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Title="DrawingView">

<ScrollView>
<Grid RowDefinitions="40,40,40,40,40,40,40,40,200,200,100" ColumnDefinitions="*,*">
<Grid RowDefinitions="40,40,40,40,40,40,40,40,40,200,200,100" ColumnDefinitions="*,*" RowSpacing="12" >
<Label Grid.Row="0" Grid.Column="0" Text="Clear on Finish" Margin="0,0,5,0" HorizontalOptions="End" HorizontalTextAlignment="End"/>
<Switch Grid.Row="0" Grid.Column="1" x:Name="ClearOnFinish" HorizontalOptions="Start" />
<Label Grid.Row="1" Grid.Column="0" Text="Multi-Line Mode" Margin="0,0,5,0" HorizontalOptions="End" HorizontalTextAlignment="End"/>
Expand Down Expand Up @@ -51,13 +51,21 @@
Clicked="GenerateImageButtonClicked"
Text="Generate Image with Random Lines"
TextColor="Black" />

<Button
Grid.Row="7" Grid.Column="0"
Grid.ColumnSpan="2"
BackgroundColor="White"
Command="{Binding SaveCommand}"
Text="Save image"
TextColor="Black" />

<Label Text="DrawingView"
Grid.Row="7" Grid.Column="0"
Grid.Row="8" Grid.Column="0"
Grid.ColumnSpan="2"/>
<mct:DrawingView x:Name="DrawingViewControl"
Margin="0,0,0,10"
Grid.Row="8" Grid.Column="0"
Grid.Row="9" Grid.Column="0"
Grid.ColumnSpan="2"
LineColor="Green"
LineWidth="5"
Expand All @@ -78,12 +86,12 @@
</mct:DrawingView>

<Image
Grid.Row="9" Grid.Column="0"
Grid.Row="10" Grid.Column="0"
Grid.ColumnSpan="2"
x:Name="GestureImage"
Margin="0,0,0,10"/>

<Label Grid.Row="10" Grid.Column="0"
<Label Grid.Row="11" Grid.Column="0"
Grid.ColumnSpan="2"
BackgroundColor="Gray"
TextColor="Black"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
using System.Collections.ObjectModel;
using System.Windows.Input;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Core.Views;
using CommunityToolkit.Maui.Storage;
using CommunityToolkit.Maui.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace CommunityToolkit.Maui.Sample.ViewModels.Views;

public partial class DrawingViewViewModel : BaseViewModel
{
readonly IFileSaver fileSaver;

[ObservableProperty]
string logs = string.Empty;

public DrawingViewViewModel()
public DrawingViewViewModel(IFileSaver fileSaver)
{
this.fileSaver = fileSaver;

DrawingLineCompletedCommand = new Command<IDrawingLine>(line => Logs = "GestureCompletedCommand executed." + Environment.NewLine + $"Line points count: {line.Points.Count}" + Environment.NewLine + Environment.NewLine + Logs);

ClearLinesCommand = new Command(Lines.Clear);
Expand Down Expand Up @@ -53,4 +60,26 @@ public static IEnumerable<PointF> GeneratePoints(int count, double viewWidth, do
yield return new PointF(Random.Shared.Next(1, maxWidthInt), Random.Shared.Next(1, maxHeightInt));
}
}

[RelayCommand]
public async Task Save(CancellationToken cancellationToken)
{
try
{
await using var stream = await DrawingView.GetImageStream(Lines, new Size(1920, 1080), Brush.Blue);

await Permissions.RequestAsync<Permissions.StorageRead>().WaitAsync(cancellationToken);
await Permissions.RequestAsync<Permissions.StorageWrite>().WaitAsync(cancellationToken);

await fileSaver.SaveAsync("drawing.png", stream, cancellationToken);
}
catch (PermissionException e)
{
await Toast.Make($"Save Failed: {e.Message}").Show(cancellationToken);
}
catch (InvalidOperationException)
{
await Toast.Make("Save Failed: No Lines Drawn").Show(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public interface IDrawingView : IView
/// <summary>
/// Retrieves a <see cref="Stream"/> containing an image of the <see cref="Lines"/> that are currently drawn on the <see cref="IDrawingView"/>.
/// </summary>
/// <param name="imageSizeWidth">Desired width of the image that is returned.</param>
/// <param name="imageSizeHeight">Desired height of the image that is returned.</param>
/// <param name="imageSizeWidth">Desired width of the image that is returned. The image will be resized proportionally.</param>
/// <param name="imageSizeHeight">Desired height of the image that is returned. The image will be resized proportionally.</param>
/// <returns><see cref="Task{Stream}"/> containing the data of the requested image with data that's currently on the <see cref="IDrawingView"/>.</returns>
ValueTask<Stream> GetImageStream(double imageSizeWidth, double imageSizeHeight);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class DrawingViewService
/// Get image stream from lines
/// </summary>
/// <param name="lines">Drawing lines</param>
/// <param name="imageSize">Image size</param>
/// <param name="imageSize">Maximum image size. The image will be resized proportionally.</param>
/// <param name="background">Image background</param>
/// <returns>Image stream</returns>
public static ValueTask<Stream> GetImageStream(in IList<IDrawingLine> lines,
Expand All @@ -33,7 +33,7 @@ public static ValueTask<Stream> GetImageStream(in IList<IDrawingLine> lines,
/// Get image stream from points
/// </summary>
/// <param name="points">Drawing points</param>
/// <param name="imageSize">Image size</param>
/// <param name="imageSize">Maximum image size. The image will be resized proportionally.</param>
/// <param name="lineWidth">Line Width</param>
/// <param name="strokeColor">Line color</param>
/// <param name="background">Image background</param>
Expand Down Expand Up @@ -168,14 +168,10 @@ static void DrawStrokes(Canvas canvas, ICollection<PointF> points, float lineWid
static Bitmap GetMaximumBitmap(in Bitmap sourceImage, in float maxWidth, in float maxHeight)
{
var sourceSize = new Size(sourceImage.Width, sourceImage.Height);
var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
if (maxResizeFactor > 1)
{
return sourceImage;
}

var width = maxResizeFactor * sourceSize.Width;
var height = maxResizeFactor * sourceSize.Height;
var maxResizeFactor = Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);

var width = Math.Max(maxResizeFactor * sourceSize.Width, 1);
var height = Math.Max(maxResizeFactor * sourceSize.Height, 1);
return Bitmap.CreateScaledBitmap(sourceImage, (int)width, (int)height, false)
?? throw new InvalidOperationException("Failed to create Bitmap");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class DrawingViewService
/// Get image stream from lines
/// </summary>
/// <param name="lines">Drawing lines</param>
/// <param name="imageSize">Image size</param>
/// <param name="imageSize">Maximum image size. The image will be resized proportionally.</param>
/// <param name="background">Image background</param>
/// <returns>Image stream</returns>
public static ValueTask<Stream> GetImageStream(in IList<IDrawingLine> lines, in Size imageSize, in Paint? background)
Expand Down Expand Up @@ -120,14 +120,10 @@ static void DrawStrokes(CGContext context, IList<PointF> points, NFloat lineWidt
static UIImage GetMaximumUIImage(UIImage sourceImage, double maxWidth, double maxHeight)
{
var sourceSize = sourceImage.Size;
var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width.Value, maxHeight / sourceSize.Height.Value);
if (maxResizeFactor > 1)
{
return sourceImage;
}

var width = maxResizeFactor * sourceSize.Width.Value;
var height = maxResizeFactor * sourceSize.Height.Value;
var maxResizeFactor = Math.Min(maxWidth / sourceSize.Width.Value, maxHeight / sourceSize.Height.Value);

var width = Math.Max(maxResizeFactor * sourceSize.Width.Value, 1);
var height = Math.Max(maxResizeFactor * sourceSize.Height.Value, 1);

UIGraphics.BeginImageContext(new CGSize(width, height));
sourceImage.Draw(new CGRect(0, 0, width, height));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class DrawingViewService
/// Get image stream from points
/// </summary>
/// <param name="points">Drawing points</param>
/// <param name="imageSize">Image size</param>
/// <param name="imageSize">Maximum image size. The image will be resized proportionally.</param>
/// <param name="lineWidth">Line Width</param>
/// <param name="strokeColor">Line color</param>
/// <param name="background">Image background</param>
Expand All @@ -21,7 +21,7 @@ public static ValueTask<Stream> GetImageStream(IList<PointF> points, Size imageS
/// Get image stream from lines
/// </summary>
/// <param name="lines">Drawing lines</param>
/// <param name="imageSize">Image size</param>
/// <param name="imageSize">Maximum image size. The image will be resized proportionally.</param>
/// <param name="background">Image background</param>
/// <returns>Image stream</returns>
public static ValueTask<Stream> GetImageStream(IList<IDrawingLine> lines, Size imageSize, Paint? background)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class DrawingViewService
/// Get image stream from points
/// </summary>
/// <param name="points">Drawing points</param>
/// <param name="imageSize">Image size</param>
/// <param name="imageSize">Maximum image size. The image will be resized proportionally.</param>
/// <param name="lineWidth">Line Width</param>
/// <param name="strokeColor">Line color</param>
/// <param name="background">Image background</param>
Expand Down Expand Up @@ -44,7 +44,7 @@ public static ValueTask<Stream> GetImageStream(IList<PointF> points, Size imageS
/// Get image stream from lines
/// </summary>
/// <param name="lines">Drawing lines</param>
/// <param name="imageSize">Image size</param>
/// <param name="imageSize">Maximum image size. The image will be resized proportionally.</param>
/// <param name="background">Image background</param>
/// <returns>Image stream</returns>
public static ValueTask<Stream> GetImageStream(IList<IDrawingLine> lines, Size imageSize, Paint? background)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class DrawingViewService
/// Get image stream from lines
/// </summary>
/// <param name="lines">Drawing lines</param>
/// <param name="imageSize">Image size</param>
/// <param name="imageSize">Maximum image size. The image will be resized proportionally.</param>
/// <param name="background">Image background</param>
/// <returns>Image stream</returns>
public static ValueTask<Stream> GetImageStream(IList<IDrawingLine> lines, Size imageSize, Paint? background)
Expand All @@ -32,7 +32,7 @@ public static ValueTask<Stream> GetImageStream(IList<IDrawingLine> lines, Size i
/// Get image stream from points
/// </summary>
/// <param name="points">Drawing points</param>
/// <param name="imageSize">Image size</param>
/// <param name="imageSize">Maximum image size. The image will be resized proportionally.</param>
/// <param name="lineWidth">Line Width</param>
/// <param name="strokeColor">Line color</param>
/// <param name="background">Image background</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public Action<ICanvas, RectF>? DrawAction
/// Retrieves a <see cref="Stream"/> containing an image of the collection of <see cref="IDrawingLine"/> that is provided as a parameter.
/// </summary>
/// <param name="lines">A collection of <see cref="IDrawingLine"/> that a image is generated from.</param>
/// <param name="imageSize">The desired dimensions of the generated image.</param>
/// <param name="imageSize">The desired dimensions of the generated image. The image will be resized proportionally.</param>
/// <param name="background">Background of the generated image. If color is null, default background color is used.</param>
/// <returns><see cref="ValueTask{Stream}"/> containing the data of the requested image with data that's provided through the <paramref name="lines"/> parameter.</returns>
public static ValueTask<Stream> GetImageStream(IEnumerable<IDrawingLine> lines,
Expand All @@ -157,8 +157,8 @@ public static ValueTask<Stream> GetImageStream(IEnumerable<IDrawingLine> lines,
/// <summary>
/// Retrieves a <see cref="Stream"/> containing an image of the <see cref="Lines"/> that are currently drawn on the <see cref="DrawingView"/>.
/// </summary>
/// <param name="imageSizeWidth">Desired width of the image that is returned.</param>
/// <param name="imageSizeHeight">Desired height of the image that is returned.</param>
/// <param name="imageSizeWidth">Desired width of the image that is returned. The image will be resized proportionally.</param>
/// <param name="imageSizeHeight">Desired height of the image that is returned. The image will be resized proportionally.</param>
/// <returns><see cref="ValueTask{Stream}"/> containing the data of the requested image with data that's currently on the <see cref="DrawingView"/>.</returns>
public ValueTask<Stream> GetImageStream(double imageSizeWidth, double imageSizeHeight) => DrawingViewService.GetImageStream(Lines.ToList(), new Size(imageSizeWidth, imageSizeHeight), Background);

Expand Down