1 Star 1 Fork 0

manupstairs/WindowsDisplayScale

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Program.cs 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
Xing, Zheng 提交于 2021-12-06 18:00 . Init
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsDisplayScale
{
class Program
{
static void Main(string[] args)
{
var scaling = GetScreenScalingFactor();
Bitmap bmp = TakingScreenshotEx1(scaling);
bmp.Save("Screenshot1.png", ImageFormat.Png);
}
[DllImport("gdi32.dll", EntryPoint = "GetDeviceCaps", SetLastError = true)]
public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
enum DeviceCap
{
VERTRES = 10,
PHYSICALWIDTH = 110,
SCALINGFACTORX = 114,
DESKTOPVERTRES = 117,
// http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
}
private static double GetScreenScalingFactor()
{
var g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr desktop = g.GetHdc();
var physicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
var screenScalingFactor = (double)physicalScreenHeight / Screen.PrimaryScreen.Bounds.Height;//SystemParameters.PrimaryScreenHeight;
return screenScalingFactor;
}
private static Bitmap TakingScreenshotEx1(double scaling = 1)
{
Rectangle bounds = Screen.GetBounds(Point.Empty);
var width = (int)(bounds.Width * scaling);
var height = (int)(bounds.Height * scaling);
Bitmap bitmap = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, new Size { Width = width, Height = height });
}
return bitmap;
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/manupstairs/WindowsDisplayScale.git
git@gitee.com:manupstairs/WindowsDisplayScale.git
manupstairs
WindowsDisplayScale
WindowsDisplayScale
main

搜索帮助

Cb406eda 1850385 E526c682 1850385