Get and Set Screen Resolution

后端 未结 6 1615
灰色年华
灰色年华 2020-11-27 18:07

How can I collect and change screen resolution using Visual C#?

相关标签:
6条回答
  • 2020-11-27 18:10

    For retrieving the screen resolution, you're going to want to use the System.Windows.Forms.Screen class. The Screen.AllScreens property can be used to access a collection of all of the displays on the system, or you can use the Screen.PrimaryScreen property to access the primary display.

    The Screen class has a property called Bounds, which you can use to determine the resolution of the current instance of the class. For example, to determine the resolution of the current screen:

    Rectangle resolution = Screen.PrimaryScreen.Bounds;
    

    For changing the resolution, things get a little more complicated. This article (or this one) provides a detailed implementation and explanation. Hope this helps.

    0 讨论(0)
  • 2020-11-27 18:17

    In C# this is how to get the resolution Screen:

    button click or form load:

    string screenWidth = Screen.PrimaryScreen.Bounds.Width.ToString();
    string screenHeight = Screen.PrimaryScreen.Bounds.Height.ToString();
    Label1.Text = ("Resolution: " + screenWidth + "x" + screenHeight);
    
    0 讨论(0)
  • 2020-11-27 18:27

    This code will work perfectly in WPF. You can use it in either page load or in button click.

          string screenWidth =System.Windows.SystemParameters.PrimaryScreenWidth.ToString();
    
          string screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight.ToString();
    
          txtResolution.Text ="Resolution : "+screenWidth + " X " + screenHeight;
    
    0 讨论(0)
  • 2020-11-27 18:30

    in Winforms, there is a Screen class you can use to get data about screen dimensions and color depth for all displays connected to the computer. Here's the docs page: http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx

    CHANGING the screen resolution is trickier. There is a Resolution third party class that wraps the native code you'd otherwise hook into. Use its CResolution nested class to set the screen resolution to a new height and width; but understand that doing this will only work for height/width combinations the display actually supports (800x600, 1024x768, etc, not 817x435).

    0 讨论(0)
  • 2020-11-27 18:30

    I have spent a lot of time trying to figure this out, for devices with scaling factor is very difficult to get the actual size of the screen due to the scaling factor sometimes 125% or 150% when the calls are made to the C# objects the returning value is not the right now, so you need to make a windows API call to get the scaling factor and apply the multiplier, the only working way that I have found for non WPF apps is here

    https://stackoverflow.com/a/21450169

    0 讨论(0)
  • 2020-11-27 18:32

    If you want to collect screen resolution you can run the following code within a WPF window (the window is what the this would refer to):

    System.Windows.Media.Matrix m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
    Double dpiX = m.M11 * 96;
    Double dpiY = m.M22 * 96;
    
    0 讨论(0)
提交回复
热议问题