Click or drag to resize
CanvasImageSaveAsync Method (ICanvasImage, Rect, Single, ICanvasResourceCreator, IRandomAccessStream, CanvasBitmapFileFormat)
Saves an ICanvasImage to the given stream.

Namespace:  Microsoft.Graphics.Canvas
Assembly:  Microsoft.Graphics.Canvas (in Microsoft.Graphics.Canvas.dll) Version: 0.0.0.0
Syntax
C#
public static IAsyncAction SaveAsync(
	ICanvasImage image,
	Rect sourceRectangle,
	float dpi,
	ICanvasResourceCreator resourceCreator,
	IRandomAccessStream stream,
	CanvasBitmapFileFormat fileFormat
)

Parameters

image
Type: Microsoft.Graphics.CanvasICanvasImage
sourceRectangle
Type: Windows.FoundationRect
dpi
Type: SystemSingle
resourceCreator
Type: Microsoft.Graphics.CanvasICanvasResourceCreator
stream
Type: Windows.Storage.StreamsIRandomAccessStream
fileFormat
Type: Microsoft.Graphics.CanvasCanvasBitmapFileFormat

Return Value

Type: IAsyncAction
Remarks

This method saves the given image. The image can be any size, memory or output file format permitting, and is not restricted by the value of CanvasDevice.MaximumBitmapSizeInPixels.

The ICanvasImage may need to be drawn to a render target before it can be saved - for example, it may be an effect or a command list. SaveAsync will take care of this automatically and create whatever temporary render targets are required.

To save an image using a high dynamic range (HDR) pixel format, use SaveAsync(ICanvasImage, Rect, Single, ICanvasResourceCreator, IRandomAccessStream, CanvasBitmapFileFormat, Single, CanvasBufferPrecision) with JpegXR format and a suitable CanvasBufferPrecision.

Examples
C#
async void SaveImage(ICanvasImage image, Rect sourceRectangle)
{
    var picker = new FileSavePicker();
    picker.FileTypeChoices.Add("Images", new List<string>() { ".jpg" });

    var file = await picker.PickSaveFileAsync();
    if (file == null)
        return;

    using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        CanvasImage.SaveAsync(image, sourceRectangle, 96, this.canvas, stream, CanvasBitmapFileFormat.Jpeg);
    }
}
See Also