これは一種の 2 つの部分からなる質問です。まず、このコードが機能しないのはなぜですか?
Canvas canvas = new Canvas { Width = 640, Height = 480 };
System.Windows.Size size = new System.Windows.Size( canvas.Width, canvas.Height);
//Measure and arrange the surface
canvas.Measure( size );
canvas.Arrange( new Rect( size ) );
canvas.Background = new SolidColorBrush( Colors.Purple );
RenderTargetBitmap bitmap = new RenderTargetBitmap( (int)canvas.Width, (int)canvas.Height, 96d, 96d, PixelFormats.Pbgra32 );
bitmap.Render( canvas );
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add( BitmapFrame.Create( bitmap ) );
using ( MemoryStream outStream = new MemoryStream() )
{
encoder.Save( outStream );
outStream.Seek( 0, SeekOrigin.Begin );
BitmapImage bmp = new BitmapImage { CacheOption = BitmapCacheOption.OnLoad };
bmp.BeginInit();
bmp.StreamSource = outStream;
bmp.EndInit();
}
画像をディスクに書き込むと、黒い画像しか表示されません。以前にこれを行ったことがあり、問題はありませんでしたが、今は何かが私を逃れています...幅と高さ、およびバッファデータを確認しましたMemoryStream とすべてが問題ないように見えます...
これは単なるテストです。実際の目標は、Canvas ビジュアル イメージから BitmapSource を作成することです。キャンバスは、コード内の形状 (ポリラインなど) で描画されています。次に、この BitmapSource を xaml の Image に 1 秒あたり約 60 フレームの速度で渡す必要があります。モック BitmapSource を作成すると Image.Source が CachedBitmap を使用していることに気付きましたが、(黒の) ビットマップを更新するたびに BitmapImage に再バインドされます。
60fps でメモリ内に Canvas を作成し、Image.Source から CachedBitmap として表示される BitmapSource を作成する方法に関する提案はありますか?