.Net 如何获取缩放之前屏幕真实分辨率(屏幕缩放比例)
尤其对Win10笔记本而言,屏幕本身分辨率1920*1080;但为了看的清楚,系统显示设置里有一个“缩放与布局”,一般为125%或者150%
造成的问题:用Screen.PrimaryScreen.Bounds取得的屏幕分辨率宽度是1536的(1920 / 1.25=1536)
网上查过很多资料,多是用API的,测试也不成功。换个思路,建一个足够大的Bitmap,把屏幕CopyFromScreen填充到这个大图里,然后查一下从哪里开始屏幕Pixel的alpha=0了,这就是屏幕的真实分辨率1920
Public Function GetRealScreenSize() As Size
Dim W As Integer = Screen.PrimaryScreen.Bounds.Width
Dim H As Integer = Screen.PrimaryScreen.Bounds.Height
Dim Img As Image = New Bitmap(W * 3, H * 3)
Dim G As Graphics
G = Graphics.FromImage(Img)
G.CopyFromScreen(New Point(0, 0), New Point(0, 0), Img.Size)
G.Dispose()
GC.Collect()
For i = W To W * 3 - 1 Step 1
Dim PointColor As Color = CType(Img, Bitmap).GetPixel(i, 0)
If PointColor.A = 0 And PointColor.R = 0 And PointColor.G = 0 And PointColor.B = 0 Then
W = i
Exit For
End If
Next
If W <> Screen.PrimaryScreen.Bounds.Width Then
H = CInt(H * (W / Screen.PrimaryScreen.Bounds.Width))
End If
Return New Size(W, H)
End Function