I have done the following based on this answer :
In the view model, there is such a property that is associated with the image source in XAML:
private WriteableBitmap cameraImage; private IntPtr cameraBitmapPtr; public WriteableBitmap CameraImage { get { return cameraImage; } set { cameraImage = value; cameraBitmapPtr = cameraImage.BackBuffer; NotifyPropertyChanged(); } }
Using a property means that if WritableBitmap changes, for example. due to permission, it will be updated in the view and a new IntPtr will be created.
The image is built if necessary:
CameraImage = new WriteableBitmap(2448, 2048, 0, 0, PixelFormats.Bgr24, null);
In the update stream, a new image is copied, for example. via:
[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")] public static extern void CopyMemory(IntPtr Destination, IntPtr Source, uint Length);
would you do
CopyMemory(cameraImagePtr, newImagePtr, 2448 * 2048 * 3);
It might be better for this ...
In the same stream, after the copy:
parent.Dispatcher.Invoke(new Action(() => { cameraImage.Lock(); cameraImage.AddDirtyRect(new Int32Rect(0, 0, cameraImage.PixelWidth, cameraImage.PixelHeight)); cameraImage.Unlock(); }), DispatcherPriority.Render);
where parent
is the control / image window.