Jump to content
  • Advertisement
Sign in to follow this  
Laurent Thienard

c# Capture Process Window

This topic is 655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!