I have below code to take screenshot and it's working, but the problem is it crop the image from the buttom and save it. I want to take full desktop screenshot.
C#
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MyApp
{
class ScreenCapture
{
internal static void ScreenShort()
{
// Start the process...
Bitmap memoryImage;
memoryImage = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
Size s = new Size(memoryImage.Width, memoryImage.Height);
// Create graphics
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
// Copy data from screen
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
//That's it! Save the image in the directory and this will work like charm.
string str = "";
try
{
str = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Screenshot.png");
}
catch (Exception er)
{
Console.WriteLine("Sorry, there was an error: " + er.Message);
Console.WriteLine();
}
// Save it!
memoryImage.Save(str);
// Write the message,
Console.WriteLine("Picture has been saved...");
Console.WriteLine();
}
}
}
Any advice or suggestions would be greatly appreciated.

Amit MohantyRank #112 · 20K reputationPosted Oct 18, 2023, 3:28 AM EDT
Try this:
using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Windows.Forms; namespace MyApp { class ScreenCapture { public static void TakeScreenshot() { string fileName = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Screenshot.png"); var size = DisplayResoluton.GetDisplayResolution(); Bitmap captureBitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb); Rectangle captureRectangle = new Rectangle(0, 0, size.Width, size.Height); Graphics captureGraphics = Graphics.FromImage(captureBitmap); captureGraphics.CopyFromScreen(captureRectangle.Left, captureRectangle.Top, 0, 0, captureRectangle.Size); captureBitmap.Save(fileName, ImageFormat.Png); } } class DisplayResoluton { [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern int GetDeviceCaps(IntPtr hDC, int nIndex); public enum DeviceCap { VERTRES = 10, DESKTOPVERTRES = 117 } public static double GetWindowsScreenScalingFactor(bool percentage = true) { Graphics GraphicsObject = Graphics.FromHwnd(IntPtr.Zero); IntPtr DeviceContextHandle = GraphicsObject.GetHdc(); int LogicalScreenHeight = GetDeviceCaps(DeviceContextHandle, (int)DeviceCap.VERTRES); int PhysicalScreenHeight = GetDeviceCaps(DeviceContextHandle, (int)DeviceCap.DESKTOPVERTRES); double ScreenScalingFactor = Math.Round(PhysicalScreenHeight / (double)LogicalScreenHeight, 2); if (percentage) { ScreenScalingFactor *= 100.0; } GraphicsObject.ReleaseHdc(DeviceContextHandle); GraphicsObject.Dispose(); return ScreenScalingFactor; } public static Size GetDisplayResolution() { var sf = GetWindowsScreenScalingFactor(false); var screenWidth = Screen.PrimaryScreen.Bounds.Width * sf; var screenHeight = Screen.PrimaryScreen.Bounds.Height * sf; return new Size((int)screenWidth, (int)screenHeight); } } }