如何在Windows8.1的RenderTargetBitmap XAML中将BitmapImage转换成C#?
我试过了
// rendered is the RenderTargetBitmap
BitmapImage img = new BitmapImage();
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
await randomAccessStream.WriteAsync(await rendered.GetPixelsAsync());
randomAccessStream.Seek(0);
await img.SetSourceAsync(randomAccessStream);但它总是给我们带来错误
img.SetSourceAsync(randomAccessStream);在WPF中有很多种方法,但是在WinRT中?我怎么能这么做?
非常感谢!
发布于 2015-10-05 12:42:01
这就是使用Sharing render to bitmap image in windows phone 8.1的那个
结果发现我不能直接用
stream.WriteAsync(byteArray.AsBuffer());你必须使用位图编码器,最后的工作代码:
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
var buffer = await rendered.GetPixelsAsync();
// await stream.ReadAsync(buffer, (uint)buffer.Length, InputStreamOptions.None);
BitmapImage img = new BitmapImage();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
(uint)rendered.PixelWidth,
(uint)rendered.PixelHeight,
DisplayInformation.GetForCurrentView().LogicalDpi,
DisplayInformation.GetForCurrentView().LogicalDpi,
buffer.ToArray());
await encoder.FlushAsync();
await img.SetSourceAsync(stream);
preview.Source = img;发布于 2015-10-05 11:51:52
你试过这个吗?
var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(elementToRender);
image.Source = bitmap;参考资料:http://social.technet.microsoft.com/wiki/contents/articles/20648.using-the-rendertargetbitmap-in-windows-store-apps-with-xaml-and-c.aspx
更新:
另一个参..。可能会有帮助:
更新2 :
试试这个:
private async Task<BitmapImage> ByteArrayToBitmapImage(byte[] byteArray)
{
var bitmapImage = new BitmapImage();
var stream = new InMemoryRandomAccessStream();
await stream.WriteAsync(byteArray.AsBuffer());
stream.Seek(0);
bitmapImage.SetSource(stream);
return bitmapImage;
}参考文献:C# Windows 8 Store (Metro, WinRT) Byte array to BitmapImage
https://stackoverflow.com/questions/32947267
复制相似问题