public Bitmap Texture2DToBitmap() { return(ExtractRect(0, 0, width, height)); // Get the desktop capture screenTexture DataBox mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, MapFlags.None); //// Create Drawing.Bitmap var bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb); //不能是ARGB var boundsRect = new System.Drawing.Rectangle(0, 0, width, height); //// Copy pixels from screen capture Texture to GDI bitmap BitmapData mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat); IntPtr sourcePtr = mapSource.DataPointer; IntPtr destPtr = mapDest.Scan0; 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 bitmap.UnlockBits(mapDest); device.ImmediateContext.UnmapSubresource(screenTexture, 0); return(bitmap); }
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.Width, _outputDescription.DesktopBounds.Height, PixelFormat.Format32bppRgb); var boundsRect = new System.Drawing.Rectangle(0, 0, _outputDescription.DesktopBounds.Width, _outputDescription.DesktopBounds.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 (int y = 0; y < _outputDescription.DesktopBounds.Height; y++) { // Copy a single line Utilities.CopyMemory(destPtr, sourcePtr, _outputDescription.DesktopBounds.Width * 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; }
public static Bitmap CropCircleImage(Bitmap image) { var cropRect = new System.Drawing.Rectangle(0, 0, image.Width, image.Height); using (Bitmap cropImage = image.Clone(cropRect, image.PixelFormat)) { using (var tb = new TextureBrush(cropImage)) { var target = new Bitmap(cropRect.Width, cropRect.Height); using (Graphics g = Graphics.FromImage(target)) { g.FillEllipse(tb, new System.Drawing.Rectangle(0, 0, cropRect.Width, cropRect.Height)); var p = new Pen(System.Drawing.Color.Red, 8) { Alignment = PenAlignment.Inset }; g.DrawEllipse(p, 0, 0, cropRect.Width, cropRect.Width); return target; } } } }
public System.Drawing.Bitmap getImageFromDataBox(DataBox mapSource) { var image = new System.Drawing.Bitmap(mOutputDesc.DesktopBounds.Width, mOutputDesc.DesktopBounds.Height, PixelFormat.Format32bppRgb); var boundsRect = new System.Drawing.Rectangle(0, 0, mOutputDesc.DesktopBounds.Width, mOutputDesc.DesktopBounds.Height); var sourcePtr = mapSource.DataPointer; var mapDest = image.LockBits(boundsRect, ImageLockMode.WriteOnly, image.PixelFormat); var destPtr = mapDest.Scan0; // Test 1 unsafe { Utilities.CopyMemory(destPtr, sourcePtr, mOutputDesc.DesktopBounds.Width * mOutputDesc.DesktopBounds.Height * 4); } image.UnlockBits(mapDest); return(image); }
/// <inheritdoc /> public override void BeginScreenDeviceChange(bool willBeFullScreen) { if (gameForm != null) { oldClientSize = gameForm.ClientSize; } if (willBeFullScreen && !isFullScreenMaximized && gameForm != null) { savedFormBorderStyle = gameForm.FormBorderStyle; savedWindowState = gameForm.WindowState; savedBounds = gameForm.Bounds; if (gameForm.WindowState == FormWindowState.Maximized) { savedRestoreBounds = gameForm.RestoreBounds; } } if (willBeFullScreen != isFullScreenMaximized) { deviceChangeChangedVisible = true; oldVisible = Visible; Visible = false; if (gameForm != null) { gameForm.SendToBack(); } } else { deviceChangeChangedVisible = false; } if (!willBeFullScreen && isFullScreenMaximized && gameForm != null) { gameForm.TopMost = false; gameForm.FormBorderStyle = savedFormBorderStyle; } deviceChangeWillBeFullScreen = willBeFullScreen; }
public static Bitmap CropCircleImage(Bitmap image) { var cropRect = new System.Drawing.Rectangle(0, 0, image.Width, image.Height); using (Bitmap cropImage = image.Clone(cropRect, image.PixelFormat)) { using (var tb = new TextureBrush(cropImage)) { var target = new Bitmap(cropRect.Width, cropRect.Height); using (Graphics g = Graphics.FromImage(target)) { g.FillEllipse(tb, new System.Drawing.Rectangle(0, 0, cropRect.Width, cropRect.Height)); var p = new Pen(System.Drawing.Color.Red, 8) { Alignment = PenAlignment.Inset }; g.DrawEllipse(p, 0, 0, cropRect.Width, cropRect.Width); return(target); } } } }
public static Bitmap CaptureScreen() { // # of graphics card adapter const int numAdapter = 0; // # of output device (i.e. monitor) const int numOutput = 1; // Create DXGI Factory1 var factory = new Factory1(); var adapter = factory.GetAdapter1(numAdapter); // Create device from Adapter var device = new Device(adapter); // Get DXGI.Output var output = adapter.GetOutput(numOutput); var output1 = output.QueryInterface<Output1>(); // Width/Height of desktop to capture int width = ((Rectangle)output.Description.DesktopBounds).Width; //width = 1024; int height = ((Rectangle)output.Description.DesktopBounds).Height; //height = 1024; // Create Staging texture CPU-accessible var textureDesc = new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.Read, BindFlags = BindFlags.None, Format = Format.B8G8R8A8_UNorm, Width = width, Height = height, OptionFlags = ResourceOptionFlags.None, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Staging }; var screenTexture = new Texture2D(device, textureDesc); // Duplicate the output var duplicatedOutput = output1.DuplicateOutput(device); bool captureDone = false; Bitmap bitmap = null; for (int i = 0; !captureDone; i++) { try { SharpDX.DXGI.Resource screenResource; OutputDuplicateFrameInformation duplicateFrameInformation; // Try to get duplicated frame within given time duplicatedOutput.AcquireNextFrame(10000, out duplicateFrameInformation, out screenResource); if (i > 0) { // copy resource into memory that can be accessed by the CPU using (var screenTexture2D = screenResource.QueryInterface<Texture2D>()) device.ImmediateContext.CopyResource(screenTexture2D, screenTexture); // Get the desktop capture texture var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, MapFlags.None); // Create Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, PixelFormat.Format32bppArgb); var boundsRect = new System.Drawing.Rectangle(0, 0, width, height); // Copy pixels from screen capture Texture to GDI bitmap var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat); var sourcePtr = mapSource.DataPointer; var destPtr = mapDest.Scan0; 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 bitmap.UnlockBits(mapDest); device.ImmediateContext.UnmapSubresource(screenTexture, 0); // Capture done captureDone = true; } screenResource.Dispose(); duplicatedOutput.ReleaseFrame(); } catch (SharpDXException e) { if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code) { throw e; } } } duplicatedOutput.Dispose(); screenTexture.Dispose(); output1.Dispose(); output.Dispose(); device.Dispose(); adapter.Dispose(); factory.Dispose(); return bitmap; }
private void ProcessFrame(DesktopFrame frame) { // Get the desktop capture texture var mapSource = mDevice.ImmediateContext.MapSubresource(desktopImageTexture, 0, MapMode.Read, MapFlags.None); FinalImage = new System.Drawing.Bitmap(mOutputDesc.DesktopBounds.Width, mOutputDesc.DesktopBounds.Height, PixelFormat.Format32bppRgb); var boundsRect = new System.Drawing.Rectangle(0, 0, mOutputDesc.DesktopBounds.Width, mOutputDesc.DesktopBounds.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 (int y = 0; y < mOutputDesc.DesktopBounds.Height; y++) { // Copy a single line Utilities.CopyMemory(destPtr, sourcePtr, mOutputDesc.DesktopBounds.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(); }
private static Bitmap GetScreenBitmap(Output output) { // # of graphics card adapter //const int numAdapter = 0; // Get DXGI.Output var output1 = output.QueryInterface <Output1>(); // Width/Height of desktop to capture int width = ((Rectangle)output.Description.DesktopBounds).Width; int height = ((Rectangle)output.Description.DesktopBounds).Height; // Create Staging texture CPU-accessible var textureDesc = new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.Read, BindFlags = BindFlags.None, Format = Format.B8G8R8A8_UNorm, Width = width, Height = height, OptionFlags = ResourceOptionFlags.None, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Staging }; var screenTexture = new Texture2D(device, textureDesc); Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb); // Duplicate the output var duplicatedOutput = output1.DuplicateOutput(device); bool captureDone = false; for (int j = 0; !captureDone; j++) { try { // Try to get duplicated frame within given time duplicatedOutput.AcquireNextFrame( 500, out OutputDuplicateFrameInformation duplicateFrameInformation, out SharpDX.DXGI.Resource screenResource); if (j > 0) { // copy resource into memory that can be accessed by the CPU using (var screenTexture2D = screenResource.QueryInterface <Texture2D>()) device.ImmediateContext.CopyResource(screenTexture2D, screenTexture); // Get the desktop capture texture var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None); // Create Drawing.Bitmap var boundsRect = new System.Drawing.Rectangle(0, 0, width, height); // Copy pixels from screen capture Texture to GDI bitmap var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat); var sourcePtr = mapSource.DataPointer; var destPtr = mapDest.Scan0; 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 bitmap.UnlockBits(mapDest); device.ImmediateContext.UnmapSubresource(screenTexture, 0); // Capture done captureDone = true; } screenResource.Dispose(); duplicatedOutput.ReleaseFrame(); } catch (SharpDXException e) { if (e.ResultCode.Code == SharpDX.DXGI.ResultCode.AccessLost.Result.Code) { duplicatedOutput.Dispose(); duplicatedOutput = output1.DuplicateOutput(device); } else if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code) { throw e; } } } duplicatedOutput.Dispose(); screenTexture.Dispose(); output1.Dispose(); output.Dispose(); return(bitmap); }
/// <summary> /// Creates and places the regions needed to be drawn to render the frame /// </summary> /// <param name="frameElement"> /// XML node for the frame containing the region /// </param> /// <param name="bitmaps"> /// Bitmap lookup table to associate a region's bitmap id to the real bitmap /// </param> /// <returns>The regions created for the frame</returns> private Frame.Region[] createAndPlaceRegions( XElement frameElement, IDictionary <string, SpriteTexture> bitmaps ) { var regions = new List <Frame.Region>(); // Fill all regions making up the current frame foreach (XElement element in frameElement.Descendants("region")) { XAttribute idAttribute = element.Attribute("id"); string id = (idAttribute == null) ? null : idAttribute.Value; string source = element.Attribute("source").Value; string hplacement = element.Attribute("hplacement").Value; string vplacement = element.Attribute("vplacement").Value; string x = element.Attribute("x").Value; string y = element.Attribute("y").Value; string w = element.Attribute("w").Value; string h = element.Attribute("h").Value; var tiledAttribute = element.Attribute("tiled"); bool tiled = (tiledAttribute != null) && bool.Parse(tiledAttribute.Value); // Assign the trivial attributes var region = new Frame.Region() { Id = id, Texture = bitmaps[source], Tiled = tiled }; //region.SourceRegion.X = int.Parse(x); //region.SourceRegion.Y = int.Parse(y); //region.SourceRegion.Width = int.Parse(w); //region.SourceRegion.Height = int.Parse(h); System.Drawing.Rectangle test = new System.Drawing.Rectangle(int.Parse(x), int.Parse(y), int.Parse(w), int.Parse(h)); region.SourceRegion = new Rectangle(int.Parse(x), int.Parse(y), int.Parse(w), int.Parse(h)); // Process each region's placement and set up the unified coordinates calculateRegionPlacement( getHorizontalPlacementIndex(hplacement), int.Parse(w), this.leftBorderWidth, this.rightBorderWidth, ref region.DestinationRegion.Location.X, ref region.DestinationRegion.Size.X ); calculateRegionPlacement( getVerticalPlacementIndex(vplacement), int.Parse(h), this.topBorderWidth, this.bottomBorderWidth, ref region.DestinationRegion.Location.Y, ref region.DestinationRegion.Size.Y ); regions.Add(region); } return(regions.ToArray()); }
private void RetrieveCursorMetadata(DesktopFrame frame) { var pointerInfo = new PointerInfo(); // A non-zero mouse update timestamp indicates that there is a mouse position update and optionally a shape change //if (frameInfo.LastMouseUpdateTime == 0) // return; var updatePosition = _frameInfo.PointerPosition.Visible || (pointerInfo.WhoUpdatedPositionLast == _whichOutputDevice); // If two outputs both say they have a visible, only update if new update has newer timestamp if (_frameInfo.PointerPosition.Visible && pointerInfo.Visible && (pointerInfo.WhoUpdatedPositionLast != _whichOutputDevice) && (pointerInfo.LastTimeStamp > _frameInfo.LastMouseUpdateTime)) { updatePosition = false; } // Update position if (updatePosition) { pointerInfo.Position = new SharpDX.Point(_frameInfo.PointerPosition.Position.X, _frameInfo.PointerPosition.Position.Y); pointerInfo.WhoUpdatedPositionLast = _whichOutputDevice; pointerInfo.LastTimeStamp = _frameInfo.LastMouseUpdateTime; pointerInfo.Visible = _frameInfo.PointerPosition.Visible; } // No new shape if (_frameInfo.PointerShapeBufferSize != 0) { if (_frameInfo.PointerShapeBufferSize > pointerInfo.BufferSize) { pointerInfo.PtrShapeBuffer = new byte[_frameInfo.PointerShapeBufferSize]; pointerInfo.BufferSize = _frameInfo.PointerShapeBufferSize; } try { unsafe { fixed(byte *ptrShapeBufferPtr = pointerInfo.PtrShapeBuffer) { _deskDupl.GetFramePointerShape(_frameInfo.PointerShapeBufferSize, (IntPtr)ptrShapeBufferPtr, out pointerInfo.BufferSize, out pointerInfo.ShapeInfo); var bitmap = new Bitmap(pointerInfo.ShapeInfo.Width, pointerInfo.ShapeInfo.Height, PixelFormat.Format32bppArgb); var boundsRect = new System.Drawing.Rectangle(0, 0, pointerInfo.ShapeInfo.Width, pointerInfo.ShapeInfo.Height); var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat); var sourcePtr = (IntPtr)ptrShapeBufferPtr; var destPtr = mapDest.Scan0; for (var y = 0; y < pointerInfo.ShapeInfo.Height; y++) { // Copy a single line Utilities.CopyMemory(destPtr, sourcePtr, pointerInfo.ShapeInfo.Width * 4); // Advance pointers sourcePtr = IntPtr.Add(sourcePtr, pointerInfo.ShapeInfo.Pitch); destPtr = IntPtr.Add(destPtr, mapDest.Stride); } // Release bitmap lock bitmap.UnlockBits(mapDest); frame.CursorBitmap = bitmap; } } } catch (SharpDXException ex) { if (ex.ResultCode.Failure) { throw new DesktopDuplicationException("Failed to get frame pointer shape."); } } } //frame.CursorVisible = pointerInfo.Visible; frame.CursorLocation = new System.Drawing.Point(pointerInfo.Position.X, pointerInfo.Position.Y); }