Problems with PrimaryScreen.Size

后端 未结 2 1672
鱼传尺愫
鱼传尺愫 2021-01-04 02:49

I\'ve been fine with Screen.PrimaryScreen.Bounds.Size for some time, but on my Windows7 computer attached to my big-screen TV it is giving me incorrect values.<

相关标签:
2条回答
  • 2021-01-04 03:02

    I don't feel this is a duplicate question, but the answer is the same as on another thread: https://stackoverflow.com/a/13228495/353147 As the question isn't about blurry fonts but why Screen.PrimaryScreen.Bounds.Size returns faulty information. It could help others.

    I did run into an error message, that mscorlib threw an null error. From this thread http://forums.asp.net/t/1653876.aspx/1 I was able to discover that unchecking "Enable ClickOnce security settings" fixed it. This seems like a hack, but it works.

    0 讨论(0)
  • 2021-01-04 03:26

    It could be your Dpi setting in windows set above 100%

    Try using this method, this will scale the resolution to the current system Dpi settings

    Winforms:

    private Size GetDpiSafeResolution()
    {
        using (Graphics graphics = this.CreateGraphics())
        {
            return new Size((Screen.PrimaryScreen.Bounds.Width * (int)graphics.DpiX) / 96
              , (Screen.PrimaryScreen.Bounds.Height * (int)graphics.DpiY) / 96);
        }
    }
    

    WPF:

    private Size GetDpiSafeResolution()
    {
        PresentationSource _presentationSource = PresentationSource.FromVisual(Application.Current.MainWindow);
        Matrix matix = _presentationSource.CompositionTarget.TransformToDevice;
        return new System.Windows.Size(
            System.Windows.SystemParameters.PrimaryScreenWidth * matix.M22,
            System.Windows.SystemParameters.PrimaryScreenHeight * matix.M11);
    }
    

    Note: Make sure your MainWindow is loaded before running this code

    0 讨论(0)
提交回复
热议问题