Reputation: 3318
I'm trying to copy from the screen the position of a PictureBox
. My code looks like this:
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;
}
}
I'm getting the pictureBox1
rectangle and then copying from the screen, but it looks like its not working. If I try the following code:
g.CopyFromScreen(
camLocation.X,
121,
0,
0,
new Size(camBox.Width, camBox.Height)
);
where 121
is a random number it works (I get an image, not the part I want, but it works) so the Y coordinate of the rectangle may be wrong? Or I'm missing something...
Upvotes: 2
Views: 97
Reputation: 4542
This will get you what is behind the PictureBox
, with the opacity trick. The rest you can easily transfer to your code:
//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;
You can test this code by creating a new WinForms project, add a Button
and a PictureBox
, and place this code in the Button
's Click
event handler.
Upvotes: 1