Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

c# - Determine screen size with .NET Core 5?

How to determine the screen size with .NET Core? I want to take a screenshot as described in DotNetCore Capture A Screenshot in Windows but they use a fixed 1920*1080 resolution which does not work for my purposes.

Do I have to reference System.Windows.Forms and thereby being nailed down to Windows?

Before switching to .NET Core we used System.Windows.Forms like so:

new Size
{
  Width = Screen.AllScreens.Sum(s => s.Bounds.Width),
  Height = Screen.AllScreens.Max(s => s.Bounds.Height)
}

Which is something we could use again, but we would love something platform-independent of course.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The comments hint that it is not supported yet.

So I show you what we have done to implement it using Windows Forms (took me some time to understand how to reference Windows Forms in a .NET Core library project correctly):

You can add the Windows Forms dependency like so to the csproj file:

<ItemGroup>
  <FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" />
</ItemGroup>

Then you can use the Screen class again:

new Size
{
  Width = Screen.AllScreens.Sum(s => s.Bounds.Width),
  Height = Screen.AllScreens.Max(s => s.Bounds.Height)
}

IMPORTANT: This works on Windows only, but at least it is .NET Core compatible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...