Reputation: 381
I am working on a solution which grabs the screenshot and saves it in the form of image at regular intervals. This application is built in Windows Forms.
I have used the below code to get the screen resolution -:
int h = Screen.PrimaryScreen.WorkingArea.Height;
int w = Screen.PrimaryScreen.WorkingArea.Width;
This works fine in a laptop with 1366 * 768 resolution.
But the image gets cropped off from right and bottom side when the same application is executed on a very large monitor.
Is there a way to handle the monitor size in the code.
Upvotes: 3
Views: 1773
Reputation: 65544
This code does multiple screens... its what I use...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;
namespace JeremyThompsonLabs
{
public class Screenshot
{
public static string TakeScreenshotReturnFilePath()
{
int screenLeft = SystemInformation.VirtualScreen.Left;
int screenTop = SystemInformation.VirtualScreen.Top;
int screenWidth = SystemInformation.VirtualScreen.Width;
int screenHeight = SystemInformation.VirtualScreen.Height;
// Create a bitmap of the appropriate size to receive the screenshot.
using (Bitmap bitmap = new Bitmap(screenWidth, screenHeight))
{
// Draw the screenshot into our bitmap.
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(screenLeft, screenTop, 0, 0, bitmap.Size);
}
var uniqueFileName = Path.Combine(System.IO.Path.GetTempPath(), Path.GetRandomFileName().Replace(".", string.Empty) + ".jpeg");
try
{
bitmap.Save(uniqueFileName, ImageFormat.Jpeg);
}
catch (Exception ex)
{
return string.Empty;
}
return uniqueFileName;
}
}
}
}
Upvotes: 0
Reputation: 11801
Assuming that you want to capture the screen containing the form, use the Screen.FromControl method, passing it the form instance, and then use the WorkingArea of that screen.
If this assumption is wrong, please add more detail to your question.
Upvotes: 2