gpt4 book ai didi

c# - 捕获屏幕并放入 PictureBox

转载 作者:行者123 更新时间:2023-12-02 19:43:20 30 4
gpt4 key购买 nike

我正在使用 C# 开发一个工具,用于捕获屏幕的一部分并将其放入 PicBox 中,我已经能够获取屏幕截图,但我在某一点上遇到了麻烦。我拍摄的镜头是从屏幕的开头拍摄的,我想给出镜头应该开始的坐标(X,Y)。这是我的代码,有什么建议吗?

#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

}

非常感谢,我可能错过了一些愚蠢的东西,请给我一盏灯。

最佳答案

问题是您的 BitBlt 指定 (0,0) 作为源 X-Y 位置。更改此行:

BitBlt(dstHdc, 0, 0, boundsOfShot.Width, boundsOfShot.Height, hdc, 
0, 0, RasterOperation.SRC_COPY);

BitBlt(dstHdc, 0, 0, boundsOfShot.Width, boundsOfShot.Height, hdc, 
_start.X, _start.Y, RasterOperation.SRC_COPY);

修复了代码问题。

如果您需要某个控件的屏幕位置(与 Location 属性不同),您可以使用:

Point _start = this.imageOriginalPicBox.PointToScreen(this.imageOriginalPicBox.Location);

上面的解决方案适用于紧凑框架,但如果您使用完整框架,则有点过于复杂。为了完整起见,这里有两种更好的方法。

正如 Yorye Nathan 所指出的在注释中,更简单的方法是使用 Graphics 类的 CopyFromScreen 方法。这将删除所有 p/Invokes,并将捕获代码减少为:

Rectangle boundsOfShot = new Rectangle(_start.X, _start.Y,
_end.X - _start.X, _end.Y - _start.Y);

Bitmap shotTook = new Bitmap(boundsOfShot.Width, boundsOfShot.Height,
PixelFormat.Format16bppRgb565);

using (Graphics draw = Graphics.FromImage(shotTook))
{
draw.CopyFromScreen(_start, new Point(0, 0), boundsOfShot.Size);
}

或者,如果您 try catch 自己的控件之一的位图:

Bitmap b = new Bitmap(this.imageOriginalPicBox.Width, this.imageOriginalPicBox.Height);
this.imageOriginalPicBox.DrawToBitmap(b,
new Rectangle(new Point(0, 0), this.imageOriginalPicBox.Size));

关于c# - 捕获屏幕并放入 PictureBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33318782/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com