Take Screenshot in C#

First Set Process DPI Aware:

Get Screen Size on C#
https://pupli.net/2020/03/get-screen-size-on-c/

Install-Package System.Drawing.Common -Version 7.0.0

Take screenshot and save to file

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ScreenshotExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the bounds of the screen
            Rectangle bounds = Screen.PrimaryScreen.Bounds;

            // Create a bitmap object to store the screenshot
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                // Create a graphics object from the bitmap
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // Capture the screen
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }

                // Save the screenshot as a PNG file
                bitmap.Save("screenshot.png");
            }

            Console.WriteLine("Screenshot saved as screenshot.png");
        }
    }
}

Take screenshot and save to memory

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ScreenshotToMemory
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the bounds of the screen
            Rectangle bounds = Screen.PrimaryScreen.Bounds;

            // Create a bitmap object to store the screenshot
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                // Create a graphics object from the bitmap
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // Capture the screen
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }

                // Store the screenshot in memory using MemoryStream
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

                    // Convert the MemoryStream to byte array
                    byte[] screenshotBytes = memoryStream.ToArray();

                    // Now the screenshot is stored in screenshotBytes, you can use it as needed
                    // For demonstration, let's output the length of the byte array
                    Console.WriteLine($"Screenshot captured. Byte array length: {screenshotBytes.Length}");
                }
            }
        }
    }
}