private void ProcessFrame(DesktopFrame frame) { // Get the desktop capture texture var mapSource = _device.ImmediateContext.MapSubresource(_desktopImageTexture, 0, MapMode.Read, MapFlags.None); FinalImage = new Bitmap( _outputDescription.DesktopBounds.GetWidth(), _outputDescription.DesktopBounds.GetHeight(), PixelFormat.Format32bppRgb); var boundsRect = new System.Drawing.Rectangle(0, 0, _outputDescription.DesktopBounds.GetWidth(), _outputDescription.DesktopBounds.GetHeight()); // 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 (int y = 0; y < _outputDescription.DesktopBounds.GetHeight(); y++) { // Copy a single line Utilities.CopyMemory(destPtr, sourcePtr, _outputDescription.DesktopBounds.GetWidth() * 4); // Advance pointers sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch); destPtr = IntPtr.Add(destPtr, mapDest.Stride); } // Release source and dest locks FinalImage.UnlockBits(mapDest); _device.ImmediateContext.UnmapSubresource(_desktopImageTexture, 0); frame.DesktopImage = FinalImage; }
private Bitmap ProcessFrame(Bitmap reusableImage) { // Get the desktop capture texture var mapSource = _device.ImmediateContext.MapSubresource(_stagingTexture, 0, MapMode.Read, MapFlags.None); Bitmap image; var width = _outputDescription.DesktopBounds.GetWidth() / scalingFactor; var height = _outputDescription.DesktopBounds.GetHeight() / scalingFactor; if (reusableImage != null && reusableImage.Width == width && reusableImage.Height == height) { image = reusableImage; } else { image = new Bitmap(width, height, PixelFormat.Format32bppRgb); } var boundsRect = new System.Drawing.Rectangle(0, 0, width, height); // Copy pixels from screen capture Texture to GDI bitmap var mapDest = image.LockBits(boundsRect, ImageLockMode.WriteOnly, image.PixelFormat); var sourcePtr = mapSource.DataPointer; var destPtr = mapDest.Scan0; if (mapSource.RowPitch == mapDest.Stride) { //fast copy Utilities.CopyMemory(destPtr, sourcePtr, height * mapDest.Stride); } else { //safe copy for (int y = 0; y < height; y++) { // Copy a single line Utilities.CopyMemory(destPtr, sourcePtr, width * 4); // Advance pointers sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch); destPtr = IntPtr.Add(destPtr, mapDest.Stride); } } // Release source and dest locks image.UnlockBits(mapDest); _device.ImmediateContext.UnmapSubresource(_stagingTexture, 0); return(image); }