private void ProcessFrame(DesktopFrame frame) { // Get the desktop capture texture var mapSource = _mDevice.ImmediateContext.MapSubresource(_desktopImageTexture, 0, MapMode.Read, MapFlags.None); var bounds = (Rectangle)_mOutputDesc.DesktopBounds; FinalImage = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb); var boundsRect = new global::System.Drawing.Rectangle(0, 0, bounds.Width, bounds.Height); // Copy pixels from screen capture Texture to GDI bitmap var mapDest = FinalImage.LockBits(boundsRect, ImageLockMode.WriteOnly, FinalImage.PixelFormat); var sourcePtr = mapSource.DataPointer; var destPtr = mapDest.Scan0; for (var y = 0; y < bounds.Height; y++) { // Copy a single line Utilities.CopyMemory(destPtr, sourcePtr, bounds.Width * 4); // Advance pointers sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch); destPtr = IntPtr.Add(destPtr, mapDest.Stride); } // Release source and dest locks FinalImage.UnlockBits(mapDest); _mDevice.ImmediateContext.UnmapSubresource(_desktopImageTexture, 0); frame.DesktopImage = (Bitmap)FinalImage.Clone(); }
/// <summary> /// Retrieves the latest desktop image and associated metadata. /// </summary> public DesktopFrame GetLatestFrame() { var frame = new DesktopFrame(); // Try to get the latest frame; this may timeout var retrievalTimedOut = RetrieveFrame(); if (retrievalTimedOut) { return(null); } try { RetrieveFrameMetadata(frame); //we don't need cursor info //RetrieveCursorMetadata(frame); //we dont need a full frame //ProcessFrame(frame); } catch { ReleaseFrame(); } try { ReleaseFrame(); } catch { // throw new DesktopDuplicationException("Couldn't release frame."); } return(frame); }
private void RetrieveFrameMetadata(DesktopFrame frame) { if (_frameInfo.TotalMetadataBufferSize > 0) { // Get moved regions var movedRegionsLength = 0; var movedRectangles = new OutputDuplicateMoveRectangle[_frameInfo.TotalMetadataBufferSize]; _mDeskDupl.GetFrameMoveRects(movedRectangles.Length, movedRectangles, out movedRegionsLength); frame.MovedRegions = new MovedRegion[movedRegionsLength / Marshal.SizeOf(typeof(OutputDuplicateMoveRectangle))]; for (var i = 0; i < frame.MovedRegions.Length; i++) { var destRect = (Rectangle)movedRectangles[i].DestinationRect; frame.MovedRegions[i] = new MovedRegion { Source = new Point(movedRectangles[i].SourcePoint.X, movedRectangles[i].SourcePoint.Y), Destination = new global::System.Drawing.Rectangle(destRect.X, destRect.Y, destRect.Width, destRect.Height) }; } // Get dirty regions var dirtyRegionsLength = 0; var dirtyRectangles = new RawRectangle[_frameInfo.TotalMetadataBufferSize]; _mDeskDupl.GetFrameDirtyRects(dirtyRectangles.Length, dirtyRectangles, out dirtyRegionsLength); frame.UpdatedRegions = new global::System.Drawing.Rectangle[dirtyRegionsLength / Marshal.SizeOf(typeof(Rectangle))]; frame.FinishedRegions = new FinishedRegions[frame.UpdatedRegions.Length]; for (var i = 0; i < frame.UpdatedRegions.Length; i++) { var dirtyRect = (Rectangle)dirtyRectangles[i]; var rect = new global::System.Drawing.Rectangle(dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height); frame.FinishedRegions[i] = new FinishedRegions { Destination = rect, Frame = ExtractRect(rect.X, rect.Y, rect.Width, rect.Height) }; } } else { frame.MovedRegions = new MovedRegion[0]; frame.UpdatedRegions = new global::System.Drawing.Rectangle[0]; } }