I am using C# to develop a tool for capturing a piece of a screen and placing it into a PicBox, I am already able to get the screenshot but I am having trouble in one point. The shot that I take is being taken from the start of the screen, I want to give the coordinates(X,Y) from where the shot should start. Here is my code, any sugestions?
#region DLLIMPORTS
enum RasterOperation : uint { SRC_COPY = 0x00CC0020 }
[DllImport("coredll.dll")]
static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, RasterOperation rasterOperation);
[DllImport("coredll.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("coredll.dll")]
private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
#endregion
public PrtScreenFrm()
{
InitializeComponent();
}
private void MakeLayout()
{
this.imageOriginalPicBox.Image = PrtScreenProjectCF.Properties.Resources.testImage;
this.imageOriginalPicBox.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void PrtScreenFrm_Load(object sender, EventArgs e)
{
this.MakeLayout();
}
private void TakeScreenShot(Point _start, Point _end)
{
Rectangle boundsOfShot = new Rectangle((int)_start.X, (int)_start.Y, (int)(_end.X - _start.X), (int)(_end.Y - _start.Y));
IntPtr hdc = GetDC(IntPtr.Zero);
Bitmap shotTook = new Bitmap(boundsOfShot.Width, boundsOfShot.Height, PixelFormat.Format16bppRgb565);
using (Graphics draw = Graphics.FromImage(shotTook))
{
IntPtr dstHdc = draw.GetHdc();
BitBlt(dstHdc, 0, 0, boundsOfShot.Width, boundsOfShot.Height, hdc, 0, 0,
RasterOperation.SRC_COPY);
draw.ReleaseHdc(dstHdc);
}
imagePrintedPicBox.Image = shotTook;
imagePrintedPicBox.SizeMode = PictureBoxSizeMode.StretchImage;
ReleaseDC(IntPtr.Zero, hdc);
}
private void takeShotMenuItem_Click(object sender, EventArgs e)
{
Point start = new Point(3, 3); //here I am forcing the rectangle
Point end = new Point(237, 133); //to start where the picBox starts
TakeScreenShot(start, end); //but it doesn't work
}
Thanks a lot, I am probably missing something silly, give me a light please.