gpt4 book ai didi

c# - 我将如何为我的应用程序窗口的一部分拍摄快照?

转载 作者:太空宇宙 更新时间:2023-11-03 16:50:53 24 4
gpt4 key购买 nike

我目前正在尝试从指定的起始坐标(这是我的问题所在)拍摄应用程序窗口的指定部分的快照。

Rectangle bounds = new Rectangle((this.Width/2)-400,(this.Height/2)-200, 800,400);

using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
IntPtr hdc = graphics.GetHdc();
PrintWindow(this.axS.Handle, hdc, 0);
graphics.ReleaseHdc(hdc);
graphics.Flush();
string file = "example.png";
bitmap.Save(file, ImageFormat.Png);
}
}

我正在尝试制作一种动态自适应方法来截取窗口中心的屏幕截图,即使在调整大小后也是如此。我不确定如何将 xy 应用于屏幕截图作为屏幕截图的起点。尺寸将始终保持 800,400,并且无论窗口大小如何,始终截取应用程序中心的屏幕截图。

每一次我尝试的尝试,位图都会截取 0 (+800), 0 (+400) 的屏幕截图,其中 0, 0 我需要更改。

Bitmap 有这个能力吗?如果没有,我可以使用什么其他方法?

最佳答案

您可以使用 SetViewportOrgEx 在 HDC 上设置原点。我发现窗口的标题栏偏离了中心点的计算,所以我也考虑到了这一点。

int x = (this.Width / 2) - 400;
int y = ((this.Height + SystemInformation.CaptionHeight) / 2) - 200;

Rectangle bounds = new Rectangle(x, y, 800, 400);

using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
IntPtr hdc = graphics.GetHdc();
POINT pt;

SetViewportOrgEx(hdc, -x, -y, out pt);

// rest as before
}
}

SetViewportOrgExPOINT 的签名:

[DllImport("gdi32.dll")]
static extern bool SetViewportOrgEx(IntPtr hdc, int X, int Y, out POINT lpPoint);

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;

public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}

public static implicit operator System.Drawing.Point(POINT p)
{
return new System.Drawing.Point(p.X, p.Y);
}

public static implicit operator POINT(System.Drawing.Point p)
{
return new POINT(p.X, p.Y);
}
}

关于c# - 我将如何为我的应用程序窗口的一部分拍摄快照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4029505/

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