Save canvas from windows store app as image file

后端 未结 3 971
渐次进展
渐次进展 2021-01-14 07:19

I\'m looking for way to save canvas from windows store app, I have found:

private void CreateSaveBitmap(Canvas canvas, string filename)
     {
       Render         


        
3条回答
  •  無奈伤痛
    2021-01-14 07:33

    Thank you, Xyroid, for your idea and structure. To get a version of CreateSaveBitmapAsync() to work on 04/24/20, I had to replace your using block with the following. The changes of your hard-coded "96"'s, and the replacement of "bytes" with its definition, are optional.

    using (Windows.Storage.Streams.IRandomAccessStream stream = await fileToWhichToSave.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
    {
        Windows.Graphics.Imaging.BitmapEncoder encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId, stream);
        encoder.SetPixelData(
            Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8,
            Windows.Graphics.Imaging.BitmapAlphaMode.Ignore,
            (uint)renderTargetBitmap.PixelWidth,
            (uint)renderTargetBitmap.PixelHeight,
            Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi,
            Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi,
            pixels.ToArray());
        await encoder.FlushAsync();
    }
    

提交回复
热议问题