5

ビットマップ画像を作成しようとしていますが、次のコードがあります。

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'

追加情報: 指定されたバッファー インデックスは、バッファー容量内にありません。

詳細にドリルダウンすると、スタック トレースに次のように表示されます。

>System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer ソース、UInt32 sourceIndex、Int32 カウント) >System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer ソース) で

ピクセル配列を抽出するこの方法は、まだ UWP に適用できますか? そうである場合、このエラー メッセージから詳細を取得する方法はありますか?

4

2 に答える 2

1

ピクセル配列を抽出するその方法は、間違いなく 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 であるため、これも 0 であることを意味しsource.Capacityます。

コードにインストルメンテーションを追加して、以下を検査することをお勧めします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
于 2015-12-16T10:50:36.713 に答える