我的目标是能够制作一个窗口的位图,并且只是该窗口。我还需要能够在该窗口内制作某些像素区域的位图。这些区域不是窗户形式或类似的东西。
我想从窗口句柄制作一个屏幕,然后将我需要的屏幕区域写入位图将是解决此问题的好方法。
这就是我所做的:
Process[] processlist = Process.GetProcesses();
String[] process = null;
process = new string[200];
int i = 0;
IntPtr handle = IntPtr.Zero;
foreach (Process theprocess in processlist)
{
process[i] = theprocess.MainWindowTitle;
if (process[i].Contains("Title of Process"))
{
handle = theprocess.MainWindowHandle;
MessageBox.Show("Handle Set");
break;
}
i++;
}
//Sets new screen from Handle
Screen thescreen = Screen.FromHandle(handle);
//Bitmap stored
Bitmap bmpScreenshot = new Bitmap(thescreen.Bounds.Width, thescreen.Bounds.Height);
//Graphics object to draw screen in bitmap
Graphics g = Graphics.FromImage(bmpScreenshot);
//Copy from screen to bitmap
g.CopyFromScreen(0, 0, 0, 0, thescreen.Bounds.Size);
然而,这只是获得整个屏幕的位图。如何将其减少到只有一个窗口的大小?
感谢。
答案 0 :(得分:0)
你可以这样做:
Bitmap bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height);
DrawToBitmap(bmpScreenshot, this.DisplayRectangle);
pictureBox1.Image = bmpScreenshot;
我尝试了上面的代码并且工作正常