Skip to content

Saving a CanvasCommandList to file #515

@ratishphilip

Description

@ratishphilip

I am trying to apply some cropping to an image, captured using MediaCapture, using the CanvasCommandList and then save it to a file.

public async Task CapturePhotoAsync(CanvasAnimatedControl canvas)
{
    var file = await KnownFolders.PicturesLibrary.CreateFileAsync("Photo.jpg", CreationCollisionOption.GenerateUniqueName);

    using (var captureStream = new InMemoryRandomAccessStream())
    {
        await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), captureStream);

        captureStream.Seek(0);

        var inputBitmap = await CanvasBitmap.LoadAsync(canvas, captureStream);
        var cl = new CanvasCommandList(canvas);
        using (var clds = cl.CreateDrawingSession())
        {
            using (clds.CreateLayer(1f, new Rect(0,0,3264,1840)))
            {
                clds.Transform = Matrix3x2.CreateRotation((float)Math.PI/2f, TransformCenter) *
                        Matrix3x2.CreateScale(1.4f, TransformCenter);
                clds.DrawImage(inputBitmap);
            }
        }

        using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            try
            {
                await CanvasImage.SaveAsync(cl, new Rect(0,0,3264,1840), 96, canvas, stream, CanvasBitmapFileFormat.Jpeg);
            }
            catch (Exception e)
            {
                var ex = e.ToString();
            }
        }
    }
}

When I am executing this method, it throws an exception at CanvasImage.SaveAsync call

System.Exception: The object was not in the correct state to process the method. (Exception from HRESULT: 0x88990001)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at CameraManager.<CapturePhotoAsync>d__56.MoveNext()

What could be the reason for this exception?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions