我使用C#開發一個捕獲一塊屏幕並將其放入PicBox的工具,我已經能夠獲取屏幕截圖,但我在某一點上遇到了麻煩。我拍攝的照片是從屏幕開始拍攝的,我想給出拍攝開始位置的座標(X,Y)。 這是我的代碼,任何sugestions?捕獲一塊屏幕並放入PictureBox
#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
}
非常感謝,我可能錯過了一些愚蠢的東西,請給我一個亮點。
'CopyFromScreen'是'Graphics'的一種方法,更易於使用。 'nXDest'和'nYDest'實際上應該是0,0,以將複製區域放在目標圖像'shotTook'的角落。它是'nXSrc'和'nYSrc',它控制你想要捕獲區域開始的屏幕的位置。 – SimpleVar
當你打電話給'BitBlt' – SimpleVar
@YoryeNathan時,你實際上會把0放到所有這些參數中。「我試着用」.CopyFromScreen()「,但我不知道它爲什麼不被識別。我認爲這是因爲它是緊湊的框架(可能)。 –