使用WritableBitmap时引发ObjectDisposedException

时间:2019-02-19 17:24:59

标签: uwp objectdisposedexception writablebitmap

我正在尝试将呈现的流从RenderTargetBitmap转换为可写位图。我在按钮单击中使用了以下代码,并将流转换为位图。如果连续转换位图意味着它会抛出ObjectDisposedException

代码段

   RenderTargetBitmap rendererShapeViewBitmap = new RenderTargetBitmap();
        InMemoryRandomAccessStream renderedStream = new InMemoryRandomAccessStream();
        await rendererShapeViewBitmap.RenderAsync(button, (int)button.Width, (int)button.Height);
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, renderedStream);
        IBuffer pixelBuffer = await rendererShapeViewBitmap.GetPixelsAsync();
        var dataWriter = new Windows.Storage.Streams.DataWriter(renderedStream);
        int heiht = rendererShapeViewBitmap.PixelHeight;
        int width = rendererShapeViewBitmap.PixelWidth;
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
            (uint)rendererShapeViewBitmap.PixelWidth, (uint)rendererShapeViewBitmap.PixelHeight, 96, 96, pixelBuffer.ToArray());
        await encoder.FlushAsync();

        var bitmap1 = new WriteableBitmap(width, heiht);
        bitmap1.SetSource(renderedStream);

        var bitmap2 = new WriteableBitmap(width,heiht);
        bitmap2.SetSource(renderedStream);

0 个答案:

没有答案