private static void RtspClient_FrameReceived(object sender, RawFrame rawFrame) { if (!(rawFrame is RawVideoFrame rawVideoFrame)) { return; } FFmpegVideoDecoder decoder = GetDecoderForFrame(rawVideoFrame); IDecodedVideoFrame decodedFrame = decoder.TryDecode(rawVideoFrame); if (decodedFrame != null) { var _FrameType = rawFrame is RawH264IFrame ? "IFrame" : "PFrame"; TransformParameters _transformParameters = new TransformParameters(RectangleF.Empty, new Size(STREAM_WIDTH, STREAM_HEIGHT), ScalingPolicy.Stretch, PixelFormat.Bgra32, ScalingQuality.FastBilinear); var pictureSize = STREAM_WIDTH * STREAM_HEIGHT; IntPtr unmanagedPointer = Marshal.AllocHGlobal(pictureSize * 4); decodedFrame.TransformTo(unmanagedPointer, STREAM_WIDTH * 4, _transformParameters); byte[] managedArray = new byte[pictureSize * 4]; Marshal.Copy(unmanagedPointer, managedArray, 0, pictureSize * 4); Marshal.FreeHGlobal(unmanagedPointer); Console.WriteLine($"Frame was successfully decoded! {_FrameType } Trying to save to JPG file..."); try { string mypath = "empty";//initialize //request.AddFile("f1", "http://*****:*****@"E:\learning\testphoto\image21.jpg", ImageFormat.Jpeg); im.Save(@mypath, ImageFormat.Jpeg); return; } if (isLinux) { // Change to your path mypath = Path.Combine(WebHostEnvironment.WebRootPath, "uploads/", "1", "image21.jpg");//linux path im.Save(@mypath, ImageFormat.Jpeg); return; } throw new PlatformNotSupportedException("Not supported OS platform!!"); } catch (Exception e) { Console.WriteLine($"Error saving to file: {e.Message}"); Debug.WriteLine($"Error saving to file: {e.Message}"); Debug.WriteLine($"Stack trace: {e.StackTrace}"); } } }
private static byte[] TransformFrame(IDecodedVideoFrame decodedFrame, Size pictureSize) { var transformParameters = new TransformParameters( RectangleF.Empty, pictureSize, ScalingPolicy.Stretch, FrameDecoderCore.PixelFormat.Bgra32, ScalingQuality.FastBilinear); var pictureArraySize = pictureSize.Width * pictureSize.Height * 4; var unmanagedPointer = Marshal.AllocHGlobal(pictureArraySize); decodedFrame.TransformTo(unmanagedPointer, pictureSize.Width * 4, transformParameters); var managedArray = new byte[pictureArraySize]; Marshal.Copy(unmanagedPointer, managedArray, 0, pictureArraySize); Marshal.FreeHGlobal(unmanagedPointer); return(managedArray); }
private void OnFrameReceived(object sender, IDecodedVideoFrame decodedFrame) { if (!decodedFrame.FrameParameters.Equals(_frameParameters)) { _frameParameters = decodedFrame.FrameParameters; _transformParameters = new TransformParameters(System.Drawing.RectangleF.Empty, new System.Drawing.Size(_frameParameters.Width, _frameParameters.Height), ScalingPolicy.Stretch, PixelFormat.Bgr24, ScalingQuality.FastBilinear); _cvBitmap = new Image <Bgr, byte>(_frameParameters.Width, _frameParameters.Height); FrameSizeChanged?.Invoke(this, new Tuple <double, double>(_frameParameters.Width, _frameParameters.Height)); } if (_yoloWrapper.IsYoloReady()) { decodedFrame.TransformTo(_cvBitmap.Mat.DataPointer, _cvBitmap.Mat.Cols * _cvBitmap.Mat.ElementSize, _transformParameters); _yoloWrapper.FrameIn(this, _cvBitmap.Mat); } }
public async Task SaveScreenCapAsync(String strPath = null, String strFileName = null) { WriteableBitmap writeableBitmap; lock (m_decoderLock) { if (m_lastDecodedFrame == null || State != EState.ACTIVE) { return; } TransformParameters transformParameters = new TransformParameters(RectangleF.Empty, new System.Drawing.Size(m_lastDecodedFrame.Parameters.Width, m_lastDecodedFrame.Parameters.Height), ScalingPolicy.RespectAspectRatio, PixelFormat.Bgra32, ScalingQuality.Bicubic); writeableBitmap = new WriteableBitmap( m_lastDecodedFrame.Parameters.Width, m_lastDecodedFrame.Parameters.Height, ScreenInfo.DpiX, ScreenInfo.DpiY, PixelFormats.Pbgra32, null); m_lastDecodedFrame.TransformTo(writeableBitmap.BackBuffer, writeableBitmap.BackBufferStride, transformParameters); writeableBitmap.Freeze(); } String strFullPath; if (strPath == null) { strPath = Properties.Settings.Default.PrefDownloadDir; if (String.IsNullOrWhiteSpace(strPath)) { strPath = ReceiveFilesManager.GetDefaultDownloadDirectory(); } } if (strFileName == null) { String strName = "wireboard-" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour + "h" + DateTime.Now.Minute + "m"; strFullPath = strPath + Path.DirectorySeparatorChar + strName + ".png"; for (int i = 2; File.Exists(strFullPath); i++) { strFullPath = strPath + Path.DirectorySeparatorChar + strName + "-" + i + ".png"; } } else { strFullPath = strPath + Path.DirectorySeparatorChar + strFileName; } try { await Task.Run(() => { BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(writeableBitmap)); using (var fileStream = new FileStream(strFullPath, FileMode.Create)) { encoder.Save(fileStream); } }); Log.i(TAG, "Saved screen capture as " + strFullPath, true); } catch (Exception e) { Log.e(TAG, "Failed to save screen capture. Error: " + e.Message, true); } }