I currently have a code in c# that gets color from a specific location (cursor position). It works well, but it works in a way that it takes a "screenshot" of full screen, not only specific window. So if I have some other windows above the one which I am monitoring I get wrong color code. Is there any simple way to modify this to check only the "screenshot" of specific window, not full screen? Here's
namespace MyApp
{
using System;
using System.Drawing;
using System.Windows.Forms;
internal class Screeny
{
private IntPtr window;
public Bitmap CaptureFromScreen(Rectangle rect)
{
Bitmap image = !(rect == Rectangle.Empty) ? new Bitmap(rect.Width, rect.Height) : new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(image);
if (rect == Rectangle.Empty)
{
graphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, image.Size, CopyPixelOperation.SourceCopy);
return image;
}
graphics.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
return image;
}
public bool ExpectColor(Point p, string rgb)
{
Color colorFromScreen = this.GetColorFromScreen(p);
string[] strArray = rgb.Split(new char[] { '.' });
return (((colorFromScreen.R.ToString() == strArray[0]) && (colorFromScreen.G.ToString() == strArray[1])) && (colorFromScreen.B.ToString() == strArray[2]));
}
public Color GetColorFromScreen(Point p)
{
Bitmap bitmap = this.CaptureFromScreen(new Rectangle(p, new Size(2, 2)));
Color pixel = bitmap.GetPixel(0, 0);
bitmap.Dispose();
return pixel;
}
public void setWindow(IntPtr window)
{
this.window = window;
}
}
}
You need to get Window Rect
that you trying to capture.
To accomplish this task you need a structure Rect
and a method that can get Rect
of your window:
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
Next, just use it:
public static Bitmap GetWindowScreenshotOfProcess(Process process)
{
var rect = new Rect();
GetWindowRect(process.MainWindowHandle, ref rect); // filling rect object
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
if (width <= 0 || height <= 0)
{
return null;
}
// Just for example, window screenshot export:
var bmp = new Bitmap(width, height);
var graphics = Graphics.FromImage(bmp);
graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
graphics.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
bmp.Save("c:\\temp\\window_screen.png");
return bmp;
}
This solution can get you screenshot only of foreground window. If you have any other windows in front of target window - you need to use inner WinAPI functions, like @vasily.sib rightly mentioned.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.