我正在尝试创建位图图像,并具有以下代码:
rendertargetBitmap rendertargetBitmap = new rendertargetBitmap(); await rendertargetBitmap.RenderAsync(uielement); IBuffer pixels = await rendertargetBitmap.GetPixelsAsync(); . . . var pixelArray = pixels.ToArray();
为了获得ToArray()扩展,我遇到了this问题.所以我补充说:
using System.Runtime.InteropServices.WindowsRuntime; // For ToArray
Exception thrown: ‘System.ArgumentException’ in
System.Runtime.WindowsRuntime.dllAdditional @R_268_4045@ion: The specified buffer index is not within the
buffer capacity.
当我深入研究细节时,它在Stack Trace中说:
at >System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source,UInt32 sourceIndex,Int32 count)
at >System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source)
解决方法
提取像素阵列的方法肯定适用于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,count); return destination; }
这些线几乎肯定会导致你的问题:
if (source.Capacity <= sourceIndex) throw new ArgumentException(SR.GetString("Argument_BufferIndexExceedsCapacity"));
…并且由于sourceIndex必须为0,这意味着source.Capacity也为0.
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。