Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert WPF Control to BitmapSource
    text
    copied!<p>This is kind of a two part question- First, why doesn't this code work?</p> <pre><code>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(); } </code></pre> <p>When I write the image to disk, all I see is a black image- I've done this before and had no problems, but now something is escaping me... I've checked the width and height and the buffer data in the MemoryStream and everything look okay...</p> <p>This is just a test, the real goal would be to create a BitmapSource from the Canvas visual image. The Canvas is getting drawn on with Shapes (polylines etc) in code. I then need to pass this BitmapSource to an Image in xaml, at a rate of about 60 frames per second. I noticed that the Image.Source is using CachedBitmap if I create a mock BitmapSource, but it is rebinding to my BitmapImage everytime I update my (black) Bitmap.</p> <p>Suggestions on how to create a Canvas in memory at 60fps and create a BitmapSource from it that is seen by Image.Source as a CachedBitmap?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload