1

ある WriteableBitmap のピクセル バッファを別の WriteableBitmap にコピーしようとしているという問題が発生しており、本質的に WriteableBitmap オブジェクトのコピーが提供されています。ただし、これを実行しようとすると、2 番目の WriteableBitmap のストリーム長が短すぎて最初の WriteableBitmap のすべての値を保持できないという問題が発生します。以下にコードを投稿しました。ウェブカメラから元のデータをキャプチャしていることに注意してください。ただし、「ps」オブジェクトのストリーム サイズを wb1 および wb2 と比較すると、ps のサイズはそれらの両方よりもはるかに小さいです。私が混乱しているのは、wb2 ストリーム サイズが wb1 より小さい理由です。助けてくれてありがとう。

private MemoryStream originalStream = new MemoryStream();
WriteableBitmap wb1 = new WriteableBitmap((int)photoBox.Width, (int)photoBox.Height);
WriteableBitmap wb2 = new WriteableBitmap((int)photoBox.Width, (int)photoBox.Height);

ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
var ps = new InMemoryRandomAccessStream();

await mc.CapturePhotoToStreamAsync(imageProperties, ps);
await ps.FlushAsync();

ps.Seek(0);

wb1.SetSource(ps);
(wb1.PixelBuffer.AsStream()).CopyTo(originalStream); // this works

originalStream.Position = 0;
originalStream.CopyTo(wb2.PixelBuffer.AsStream()); // this line gives me the error: "Unable to expand length of this stream beyond its capacity"

Image img = new Image(); 
img.Source = wb2; // my hope is to treat this as it's own entity and modify this image independently of wb1 or originalStream

photoBox.Source =wb1;
4

2 に答える 2

1

new WriteableBitmap(w, h) を実行してから SetSource() を別の解像度の画像に呼び出すと、ビットマップのサイズが変更されることに注意してください (コンストラクターで渡される wxh ではありません)。あなたの photoBox.Width/Height は、 CapturePhotoToStreamAsync() 呼び出しが返すものとは異なる可能性があります (photoBox は画面上の単なるコントロールですが、画像はデフォルトまたは事前構成されたカメラ設定でキャプチャされると想定しています)。

このようなことをするだけではどうですか

ps.Seek(0);
wb1.SetSource(ps);
ps.Seek(0);
wb2.SetSource(ps);
于 2012-08-09T18:50:27.923 に答える
1

PixelBuffer からライターを作成し、それを使用してストリームをコピーする必要があると思います。AsStream メソッドは、バッファへの書き込みではなく、バッファの読み取りに使用する必要があります。

http://social.msdn.microsoft.com/Forums/en-NZ/winappswithcsharp/thread/2b499ac5-8bc8-4259-a144-842bd756bfe2を ご覧ください。

コードの一部

于 2012-08-09T14:44:52.377 に答える