Sign in to follow this  

c# Capture Process Window

Recommended Posts

Hi all,

 

I want to capture Screen of Process Minimized with SharpDX.

 

I have already coded my apps for Capture Primary Adapter, but i need capture specific process.

 

Could you help me ?

 

My code:

// 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 = ((SharpDX.Rectangle)output.Description.DesktopBounds).Width;
            int height = ((SharpDX.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);

            // Duplicate the output
            var duplicatedOutput = output1.DuplicateOutput(device);

            bool captureDone = false;
            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
                        var bitmap = 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 = 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);

                        // Save the output
                        bitmap.Save("Screenshot.bmp", ImageFormat.Bmp);

                        // Capture done
                        captureDone = true;

                        if (bitmap.Width == 1280)
                        {
                            target = CSGOAutoAccept.Properties.Resources.target_1280;
                        }
                        else
                        {
                            target = CSGOAutoAccept.Properties.Resources.target;
                        }
                        
                        System.Drawing.Rectangle location = System.Drawing.Rectangle.Empty;
                        location = searchBitmap(target, bitmap, 0.1);

                        if (location.Width != 0)
                        {
                            Console.WriteLine("Found!");

                            int XPosition = location.X;
                            int YPosition = location.Y;
                            int Width = target.Width;
                            int Height = target.Height;

                            XClick = XPosition + (Width/2);
                            YClick = YPosition + (Height * 2);

                            if (modautomatic == 1)
                            {
                                LeftMouseClick(XClick, YClick);

                                if (init == 0)
                                {
                                    init++;

                                    Task.Delay(120000).ContinueWith(t => STOP());
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("Not Found!");
                        }
                    }

                    screenResource.Dispose();
                    duplicatedOutput.ReleaseFrame();
                }
                catch (SharpDXException error)
                {
                    if (error.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code)
                    {
                        throw error;
                    }
                }
            }

            // Display the texture using system associated viewer
            //System.Diagnostics.Process.Start(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, outputFileName)));

            factory.Dispose();
            adapter.Dispose();
            device.Dispose();
            output.Dispose();
            output1.Dispose();
            screenTexture.Dispose();
            duplicatedOutput.Dispose();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this