[英]Hiding a xaml element but also be able to render using RenderTargetBitmap in UWP
[英]Error using using RenderTargetBitmap in UWP
我正在尝试创建位图图像,并具有以下代码:
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(uielement);
IBuffer pixels = await renderTargetBitmap.GetPixelsAsync();
. . .
var pixelArray = pixels.ToArray();
为了获得ToArray()
扩展,我遇到了这个问题。 所以我补充说:
using System.Runtime.InteropServices.WindowsRuntime; // For ToArray
给我的代码。 但是,当我运行时,我收到以下错误:
抛出异常:System.Runtime.WindowsRuntime.dll中的“System.ArgumentException”
附加信息:指定的缓冲区索引不在缓冲区容量内。
当我深入研究细节时,它在Stack Trace中说:
at> System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source,UInt32 sourceIndex,Int32 count)> System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source)
这种提取像素阵列的方法是否仍适用于UWP? 如果是,是否有任何方法可以从此错误消息中获取更多详细信息?
提取像素阵列的方法肯定适用于UWP。 至于错误,反编译的ToArray()
是这样的:
public static byte[] ToArray(this IBuffer source)
{
if (source == null)
throw new ArgumentNullException("source");
return WindowsRuntimeBufferExtensions.ToArray(source, 0U, checked ((int) source.Length));
}
换句话说,它调用ToArray
重载,它接受一个起始索引和一个长度:
public static byte[] ToArray(this IBuffer source, uint sourceIndex, int count)
{
if (source == null)
throw new ArgumentNullException("source");
if (count < 0)
throw new ArgumentOutOfRangeException("count");
if (sourceIndex < 0U)
throw new ArgumentOutOfRangeException("sourceIndex");
if (source.Capacity <= sourceIndex)
throw new ArgumentException(SR.GetString("Argument_BufferIndexExceedsCapacity"));
if ((long) (source.Capacity - sourceIndex) < (long) count)
throw new ArgumentException(SR.GetString("Argument_InsufficientSpaceInSourceBuffer"));
byte[] destination = new byte[count];
WindowsRuntimeBufferExtensions.CopyTo(source, sourceIndex, destination, 0, count);
return destination;
}
这些线几乎肯定会导致你的问题:
if (source.Capacity <= sourceIndex)
throw new ArgumentException(SR.GetString("Argument_BufferIndexExceedsCapacity"));
......既然sourceIndex
必然是0,这将意味着source.Capacity
也为0。
我建议你在代码中添加一些检测来检查IBuffer
:
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(element);
IBuffer pixelBuffer = await rtb.GetPixelsAsync();
Debug.WriteLine($"Capacity = {pixelBuffer.Capacity}, Length={pixelBuffer.Length}");
byte[] pixels = pixelBuffer.ToArray();
我认为您的问题很可能发生在 ToArray
调用之前 。 我在我自己的UWP应用程序中使用完全相同的序列,获得调试输出如下:
Capacity = 216720, Length=216720
我在尝试在UWP应用程序中制作屏幕截图时遇到了同样的问题。
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(uielement);
当uielement
是Window.Current.Content
时,我给了我这个例外。
但是当我尝试的时候
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(null);
相同的代码毫无例外地工作,并给我UWP应用程序窗口的屏幕截图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.