c#抓屏功能在DPI缩放后,截到的图片不完整的问题

时间:2022-08-30 09:07:22
        /// <summary>
/// 获取屏幕快照
/// </summary>
/// <returns></returns>
public static Bitmap GetScreenSnapshot()
{
Bitmap bitmap = null;
try
{
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
//100%的时候,DPI是96;这条语句的作用时获取放大比例
float factor = g.DpiX / ;
Rectangle rc = new Rectangle(, , (int)(SystemParameters.PrimaryScreenWidth * factor), (int)(SystemParameters.PrimaryScreenHeight * factor));
bitmap = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
using (Graphics memoryGrahics = Graphics.FromImage(bitmap))
{
memoryGrahics.CopyFromScreen(rc.X, rc.Y, , , rc.Size, CopyPixelOperation.SourceCopy);
}
}
catch (Exception ex)
{
NlogHelper.Error("截屏失败", ex);
}
return bitmap;
}