我试图从屏幕上复制PictureBox
的位置。我的代码如下所示:
Form1.cs中:
_recorder = new ScreenRecord();
_recorder.StartRecording(
pictureBox1,
pictureBox1.RectangleToScreen(new Rectangle())
);
ScreenRecord.cs:
class ScreenRecord
{
private ScreenBitmap scBitmap;
public void StartRecording(PictureBox cam, Rectangle rect)
{
scBitmap = new ScreenBitmap(cam, rect);
cam.Image = scBitmap.GetBitmap();
}
}
ScreenBitmap.cs:
class ScreenBitmap
{
private PictureBox camBox;
private Rectangle camLocation;
public ScreenBitmap(PictureBox cam, Rectangle rect)
{
camBox = cam;
camLocation = rect;
}
public Bitmap GetBitmap()
{
Bitmap screenBitmap = GetScreen();
return screenBitmap;
}
private Bitmap GetScreen()
{
Bitmap scBitmap = new Bitmap(camBox.Width, camBox.Height);
Graphics g = Graphics.FromImage(scBitmap);
g.CopyFromScreen(
camLocation.X,
camLocation.Y,
0,
0,
new Size(camBox.Width, camBox.Height)
);
return scBitmap;
}
}
我正在获取pictureBox1
矩形,然后从屏幕上复制,但看起来它不起作用。如果我尝试以下代码:
g.CopyFromScreen(
camLocation.X,
121,
0,
0,
new Size(camBox.Width, camBox.Height)
);
其中121
是一个随机数它工作(我得到一个图像,而不是我想要的部分,但它有效)所以矩形的Y坐标可能是错误的?或者我错过了一些东西......
答案 0 :(得分:1)
这将通过不透明技巧为您提供PictureBox
背后的内容。其余的你可以很容易地转移到你的代码:
//just when you are about to capture screen take opacity and later restore it.
this.Opacity = 0.0;
Point first = PointToScreen(pictureBox1.Location);
Bitmap bit = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bit);
g.CopyFromScreen(first.X,first.Y, 0, 0, pictureBox1.Size);
this.Opacity = 1.0;
pictureBox1.Image = bit;
您可以通过创建新的WinForms项目,添加Button
和PictureBox
来测试此代码,并将此代码放入Button
' s {{1}事件处理程序。