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; } } } }
LockedBufferBgra8( SoftwareBitmap bitmap, BitmapBufferAccessMode access) { if (bitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8) { throw new ArgumentException("Invalid BitmapPixelFormat"); } m_buffer = bitmap.LockBuffer(access); m_byteAccess = (IClosableByteAccess)(Object)m_buffer; byte *data; uint capacity; m_byteAccess.Lock(out data, out capacity); m_locked = true; Description = m_buffer.GetPlaneDescription(0); PixelWidth = Description.Width; PixelHeight = Description.Height; Data = data + Description.StartIndex; m_rowIndex = 0; m_rowData = Data; m_rowReadableCapacity = (access == BitmapBufferAccessMode.Write ? 0 : (uint)Description.Stride); m_rowWriteableCapacity = (access == BitmapBufferAccessMode.Read ? 0 : (uint)Description.Stride); }
private SoftwareBitmap CleanSoftwareBitmap(ImageFrame[] imageFrameArray) { SoftwareBitmap CleanFrameBitmap = null; if (imageFrameArray != null) { if (imageFrameArray.Length > 1) { CleanFrameBitmap = new SoftwareBitmap(imageFrameArray[0].OriginalBitmap.BitmapPixelFormat, imageFrameArray[0].OriginalBitmap.PixelWidth, imageFrameArray[0].OriginalBitmap.PixelHeight); imageFrameArray[0].OriginalBitmap.CopyTo(CleanFrameBitmap); using (var convertedBuffer1 = CleanFrameBitmap.LockBuffer(BitmapBufferAccessMode.Read)) using (var convertedBuffer2 = imageFrameArray[1].OriginalBitmap.LockBuffer(BitmapBufferAccessMode.Read)) { IClosableByteAccess[] convertedByteAccess = new IClosableByteAccess[2]; IntPtr[] convertedBytes = new IntPtr[2]; uint[] convertedCapacity = new uint[2]; convertedByteAccess[0] = (IClosableByteAccess)(Object)convertedBuffer1; convertedByteAccess[0].Lock(out convertedBytes[0], out convertedCapacity[0]); convertedByteAccess[1] = (IClosableByteAccess)(Object)convertedBuffer2; convertedByteAccess[1].Lock(out convertedBytes[1], out convertedCapacity[1]); byte[][] convertedByteArray = new byte[2][]; convertedByteArray[0] = new byte[convertedCapacity[0]]; convertedByteArray[1] = new byte[convertedCapacity[1]]; Marshal.Copy(convertedBytes[0], convertedByteArray[0], 0, (int)convertedCapacity[0]); Marshal.Copy(convertedBytes[1], convertedByteArray[1], 0, (int)convertedCapacity[1]); byte[] lightFrame, darkFrame; if (imageFrameArray[0].FrameIllumination == LuminanceInfo.Light) { lightFrame = convertedByteArray[0]; darkFrame = convertedByteArray[1]; } else { lightFrame = convertedByteArray[1]; darkFrame = convertedByteArray[0]; } for (int i = 0; i < convertedCapacity[0]; i++) { int DestBit = (int)lightFrame[i] - (int)darkFrame[i]; lightFrame[i] = DestBit > 0 ? (byte)DestBit : (byte)0; } Marshal.Copy(lightFrame, 0, convertedBytes[0], (int)convertedCapacity[0]); convertedByteAccess[0].Unlock(); convertedByteAccess[1].Unlock(); } } } return(CleanFrameBitmap); }
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(); } } } } }