我正在尝试将渲染流转换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);