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

Include the details surrounding the new overloads for GetImageStream #469

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
55 changes: 53 additions & 2 deletions docs/maui/views/DrawingView.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ By default `DrawingView` supports only 1 line. To enable `MultiLine` set `IsMult

### XAML

```xml
```xaml
<views:DrawingView
Lines="{Binding MyLines}"
IsMultiLineModeEnabled="true"
Expand All @@ -77,12 +77,63 @@ The following screenshot shows the resulting DrawingView on Android:

![Screenshot of an DrawingView with multi-line on Android](../images/views/drawingview-multiline-android.gif "DrawingView on Android")

## Saving the result out to an image

The .NET MAUI Community Toolkit offers several options for saving the resulting drawing out to an image, these options are as follows:

### Saving from the `DrawingView`

The `DrawingView` provides the `GetImageStream` method that will generate an image and return the contents in a `Stream`.

The following example will export the drawing to an image at a desired width of 400 and a desired height of 300. The desired dimensions will be adjusted to make sure the aspect ratio of the drawing it retained.

```csharp
await drawingView.GetImageStream(desiredWidth: 400, desiredHeight: 300);
```

> [!NOTE]
> By default the `GetImageStream` method will return an image that contains the lines drawn, this will not match the full surface that user sees. In order to generate an image that directly matches the surface displayed in the application the `GetImageStream` method with the `DrawingViewOutputOption` parameter must be used.

The following example shows how to generate an image that directly matches the DrawingView surface displayed in an application:

```csharp
await drawingView.GetImageStream(desiredWidth: 400, desiredHeight: 300, imageOutputOption: DrawingViewOutputOption.FullCanvas);
```

### Saving from the `DrawingViewService`

Using the `DrawingView` methods can make it difficult to build an application using the MVVM pattern, to help deal with this the .NET MAUI Community Toolkit also provides the `DrawingViewService` class that will also allow the ability to generate an image stream.

The following example shows how to generate an image stream of a desired width of 1920 and height of 1080 and a blue background. Developers are required to provide the lines parameter which will be the lines that the user has drawn.

```csharp
await using var stream = await DrawingViewService.GetImageStream(
lines: Lines,
desiredSize: new Size(1920, 1080),
background: Brush.Blue);
```

In order to generate an image that directly matches the DrawingView surface as mentioned in the previous section the `canvasSize` parameter can also be used.

The following example shows how to provide a `canvasSize`.

```csharp
await using var stream = await DrawingViewService.GetImageStream(
lines: Lines,
desiredSize: new Size(1920, 1080),
background: Brush.Blue,
canvasSize: new Size(CanvasWidth, CanvasHeight));
```

For the purpose of this example the `CanvasWidth` and `CanvasHeight` properties have been data bound to the `Width` and `Height` properties of the `DrawingView` respectively. For the full solution please refer to the [.NET MAUI Community Toolkit Sample Application](https://github.com/CommunityToolkit/Maui/blob/main/samples/CommunityToolkit.Maui.Sample/Pages/Views/DrawingViewPage.xaml).

## Handle event when Drawing Line Completed

`DrawingView` allows to subscribe to the events like `OnDrawingLineCompleted`. The corresponding command `DrawingLineCompletedCommand` is also available.

### XAML
```xml

```xaml
<views:DrawingView
Lines="{Binding MyLines}"
DrawingLineCompletedCommand="{Binding DrawingLineCompletedCommand}"
Expand Down