在 UWP 中使用 RenderTargetBitmap 时出错 [英] Error using using RenderTargetBitmap in UWP
问题描述
我正在尝试创建位图图像,并具有以下代码:
I'm trying to create a bitmap image, and have the following code:
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(uielement);
IBuffer pixels = await renderTargetBitmap.GetPixelsAsync();
. . .
var pixelArray = pixels.ToArray();
为了获得 ToArray()
扩展,我遇到了 这个 问题.所以我补充说:
In order to get a ToArray()
extension, I came across this question. So I added:
using System.Runtime.InteropServices.WindowsRuntime; // For ToArray
我的代码.但是,当我运行时,出现以下错误:
To my code. However, when I run, I get the following error:
抛出异常:'System.ArgumentException' inSystem.Runtime.WindowsRuntime.dll
Exception thrown: 'System.ArgumentException' in System.Runtime.WindowsRuntime.dll
附加信息:指定的缓冲区索引不在缓冲容量.
Additional information: The specified buffer index is not within the buffer capacity.
当我深入了解细节时,它在堆栈跟踪中说:
When I drill into the details, it says in the Stack Trace:
at >System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source, UInt32 sourceIndex, Int32 count)在 >System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer 源)
at >System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source, UInt32 sourceIndex, Int32 count) at >System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source)
这种提取像素阵列的方法是否仍然适用于UWP?如果是,有什么办法可以从这个错误消息中获取更多详细信息?
Is this method of extracting a pixel array still applicable to the UWP? If it is, is there any way to get more detail from this error message?
推荐答案
那个提取像素数组的方法绝对适用于UWP.至于错误,反编译的ToArray()
是这样的:
That method of extracting a pixel array is definitely applicable to UWP. As for the error, the decompiled ToArray()
goes like this:
public static byte[] ToArray(this IBuffer source)
{
if (source == null)
throw new ArgumentNullException("source");
return WindowsRuntimeBufferExtensions.ToArray(source, 0U, checked ((int) source.Length));
}
换句话说,它调用带有起始索引和长度的 ToArray
重载:
In other words, it calls the ToArray
overload that takes a start index and a length:
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;
}
几乎肯定会导致您的问题的行:
The line(s) almost certainly causing your problem:
if (source.Capacity <= sourceIndex)
throw new ArgumentException(SR.GetString("Argument_BufferIndexExceedsCapacity"));
...并且由于 sourceIndex
必须为 0,这意味着 source.Capacity
也是 0.
...and since sourceIndex
is necessarily 0, that would mean that source.Capacity
is also 0.
我建议您在代码中添加一些检测以检查 IBuffer
:
I suggest you add some instrumentation to your code to inspect the 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 应用程序中使用完全相同的序列,获得如下调试输出:
I think it likely that your problem occurs before the ToArray
call. I'm using the exact same sequence in my own UWP app, getting debug output like so:
Capacity = 216720, Length=216720
这篇关于在 UWP 中使用 RenderTargetBitmap 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!