Save canvas from windows store app as image file

后端 未结 3 937
渐次进展
渐次进展 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:26

    Try this

    using System.Runtime.InteropServices.WindowsRuntime
    
    private async Task CreateSaveBitmapAsync(Canvas canvas)
    {
        if (canvas != null)
        {
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(canvas);
    
            var picker = new FileSavePicker();
            picker.FileTypeChoices.Add("JPEG Image", new string[] { ".jpg" });
            StorageFile file = await picker.PickSaveFileAsync();
            if (file != null)
            {
                var pixels = await renderTargetBitmap.GetPixelsAsync();
    
                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await
                        BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                    byte[] bytes = pixels.ToArray();
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                         BitmapAlphaMode.Ignore,
                                         (uint)canvas.Width, (uint)canvas.Height,
                                         96, 96, bytes);
    
                    await encoder.FlushAsync();
                }
            } 
        }
    }
    
    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
  • 2021-01-14 07:51

    I came across this reply and although it looked exactly what I was looking for it didn't work. Adding an error handler revealed:

    Value does not fall within the expected range.

    Finally I found that:

    (uint)canvas.Width, (uint)canvas.Height,
    

    were both 0. After replacing it with:

    (uint)canvas.ActualWidth, (uint)canvas.ActualHeight,
    

    it worked. No idea why it worked for Carlos28 and not for me, but with this change it worked for me too and so I thank Xyriod for the answer

    0 讨论(0)
提交回复
热议问题