ฉันติดตามโปรแกรม Huw Collingbourne ในการสร้างโปรแกรมจับภาพหน้าจอด้วย C# อย่างไรก็ตาม ฉันสังเกตเห็นสิ่งแปลก ๆ สองสามอย่าง และไม่ว่าฉันจะใช้โปรแกรมที่เขาสร้างขึ้นหรือโปรแกรมที่แก้ไขของฉันก็ยังเหมือนเดิม โดยเฉพาะฉันสร้างโปรแกรมที่เปิดหน้าต่างที่ให้คุณจับภาพพื้นที่ได้ ฉันคิดว่ามันเกี่ยวข้องกับการนั่งอยู่บนคอมพิวเตอร์ของฉัน แต่จำเป็นต้องรู้วิธีคาดการณ์และแก้ไขปัญหานี้หากผู้อื่นจะใช้โปรแกรมจับภาพหน้าจอของฉัน! หากจอแสดงผลของฉันสำหรับ Windows 10 ตั้งไว้ที่ 100% ฉันจะได้ค่ามากกว่าหน้าต่างที่เลือกเล็กน้อย และหากฉันตั้งค่าการแสดงผลเป็นข้อความ 125% ฉันจะได้พื้นที่ที่เลือกจำนวนมาก เหลือขนาดเริ่มต้นไว้คือขนาด 555, 484 แต่ฉันจับภาพได้ใหญ่กว่ามาก
public partial class Form1 : Form
{
//https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowrect
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect);
//ICON info
//https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getcursorinfo
[DllImport("user32.dll")]
private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
[DllImport("user32.dll")]
private static extern bool GetCursorInfo(out CURSORINFO pci);
public struct POINT
{
public Int32 x;
public Int32 y;
}
public struct ICONINFO
{
public bool fIcon;
public Int32 xHotspot;
public Int32 yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
public struct CURSORINFO
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public Point ptScreenPos;
}
GrabRegionForm grabForm;
public void GrabRect(Rectangle rect)
{
int rectWidth = rect.Width - rect.Left;
int rectHeight = rect.Height - rect.Top;
Bitmap bm = new Bitmap(rectWidth, rectHeight);
Graphics g = Graphics.FromImage(bm);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, new Size(rectWidth, rectHeight));
DrawMousePointer(g, Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
this.pb_screengrab.Image = bm;
Clipboard.SetImage(bm);
}
}
public partial class GrabRegionForm : Form
{
public Rectangle formRect;
private Form1 mainForm;
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void buttonOK_Click(object sender, EventArgs e)
{
formRect = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);
this.Hide();
mainForm.GrabRect(formRect);
Close();
}
}
ScreenGrab พร้อมจอแสดงผลที่ 100%
ScreenGrab พร้อมจอแสดงผลที่ 125%