private void CaptureBitmap(SoftwareBitmap softwareBitmap /*, CaptureSessionParameter captureSessionParameter*/, string tailName) { if (softwareBitmap != null) { var fullFileName = tailName == "Light" ? _frame1 : _frame2; using (var frameConvertedForRendering = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8)) { try { // As above, easiest way to get access to the data is to lock it using (var convertedBuffer = frameConvertedForRendering.LockBuffer(BitmapBufferAccessMode.Read)) { IClosableByteAccess convertedByteAccess = (IClosableByteAccess)(Object)convertedBuffer; IntPtr convertedBytes; uint convertedCapacity = 0; convertedByteAccess.Lock(out convertedBytes, out convertedCapacity); byte[] convertedByteArray = new byte[convertedCapacity]; Marshal.Copy(convertedBytes, convertedByteArray, 0, (int)convertedCapacity); WriteableBitmap ImageSourceInterimBitmap = new WriteableBitmap((int)softwareBitmap.PixelWidth, (int)softwareBitmap.PixelHeight, 96.0, 96.0, PixelFormats.Bgra32, null); ImageSourceInterimBitmap.WritePixels( new Int32Rect(0, 0, softwareBitmap.PixelWidth, softwareBitmap.PixelHeight), (IntPtr)convertedBytes, (int)convertedCapacity, convertedBuffer.GetPlaneDescription(0).Stride); Utilities.SaveImage(ImageSourceInterimBitmap, fullFileName); convertedByteAccess.Unlock(); } } catch (Exception ex) { throw ex; } } } }
Dispose() { m_rowIndex = -1; // Prevents further reading/writing m_rowReadableCapacity = 0; m_rowWriteableCapacity = 0; if (m_locked) { m_byteAccess.Unlock(); m_byteAccess = null; m_locked = false; } if (m_buffer != null) { m_buffer.Dispose(); m_buffer = null; } GC.SuppressFinalize(this); }
private void ProcessVideoFrame(VideoFrame videoFrame, Boolean ifIlluminationEnabled) { using (var originalBitmap = videoFrame.SoftwareBitmap) { if (originalBitmap != null) { using (var softwareBitmap = SoftwareBitmap.Convert(originalBitmap, BitmapPixelFormat.Bgra8)) { using (var convertedBuffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Read)) { IClosableByteAccess convertedByteAccess = (IClosableByteAccess)(Object)convertedBuffer; IntPtr convertedBytes; uint convertedCapacity = 0; convertedByteAccess.Lock(out convertedBytes, out convertedCapacity); //dispatcher = Dispatcher.CurrentDispatcher; dispatcher.Invoke((Action) delegate() { WriteableBitmap displayImageSourceInterimBitmap = new WriteableBitmap((int)softwareBitmap.PixelWidth, (int)softwareBitmap.PixelHeight, 96.0, 96.0, PixelFormats.Bgra32, null); displayImageSourceInterimBitmap.WritePixels( new Int32Rect(0, 0, softwareBitmap.PixelWidth, softwareBitmap.PixelHeight), (IntPtr)convertedBytes, (int)convertedCapacity, convertedBuffer.GetPlaneDescription(0).Stride); string filename = ifIlluminationEnabled ? Frame1 : Frame2; using (FileStream stream5 = new FileStream(filename, FileMode.Create)) { PngBitmapEncoder encoder5 = new PngBitmapEncoder(); encoder5.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(displayImageSourceInterimBitmap.Clone())); encoder5.Save(stream5); } }); convertedByteAccess.Unlock(); } } } } }