0

I use a IP camera and its library to get the image from the camera.

The library allows me to get the byte array or a bitmap. I wish to display the video in my Xaml window. I need something fast and I do not know how to do it.

Currently I use a Image widget and I convert my bitmap to a Bitmap Source :

            VideoWidget.Source = Imaging.CreateBitmapSourceFromHBitmap(camera.StreamUpdate().GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

It works but I think I can do something faster if I can update the bytes[] directly. Is there a way to do this ?

Thank you :)

  • I would guess that you would end up implementing exactly what the library is doing. What sort of optimisations are you going to write? – Neil Mar 11 '20 at 14:39

1 Answers1

0

You can use a System.Windows.Media.Imaging.WriteableBitmap.

To write your video samples use something like:

private void VideoSampleReady(byte[] sample, uint width, uint height, int stride, WriteableBitmap wBmp, System.Windows.Controls.Image dst)
{
    if (sample != null && sample.Length > 0)
    {
        this.Dispatcher.BeginInvoke(new Action(() =>
        {
            if (wBmp == null || wBmp.Width != width || wBmp.Height != height)
            {
                wBmp = new WriteableBitmap(
                    (int)width,
                    (int)height,
                    96,
                    96,
                    PixelFormats.Bgr24,
                    null);

                dst.Source = wBmp;
            }

            // Reserve the back buffer for updates.
            wBmp.Lock();

            Marshal.Copy(sample, 0, wBmp.BackBuffer, sample.Length);

            // Specify the area of the bitmap that changed.
            wBmp.AddDirtyRect(new Int32Rect(0, 0, (int)width, (int)height));

            // Release the back buffer and make it available for display.
            wBmp.Unlock();
        }), System.Windows.Threading.DispatcherPriority.Normal);
    }
}
sipsorcery
  • 30,273
  • 24
  • 104
  • 155