34
\$\begingroup\$

Output the device's screen resolution in the specific format of [width]x[height] where [width] is replaced by the horizontal number of pixels and [height] is replaced by the vertical number of pixels. For example, an output could read 1440x900. Should there be multiple screens, report about the primary screen. You may not change the resolution as part of your inquiry.

Here is a non‑golfed ECMAScript code snippet achieving this task (unless privacy settings kick in).

console.log(window.screen.width + 'x' + window.screen.height)

\$\endgroup\$
13
  • 17
    \$\begingroup\$ The specific output format is no fun, but it's probably too late to change now \$\endgroup\$
    – Luis Mendo
    Commented May 2, 2017 at 13:29
  • 3
    \$\begingroup\$ What should the behaviour be if multiple displays are connected? \$\endgroup\$ Commented May 2, 2017 at 13:40
  • 4
    \$\begingroup\$ I suppose we're not allowed to first change your resolution and then tell you those values, right? \$\endgroup\$ Commented May 2, 2017 at 15:12
  • 4
    \$\begingroup\$ APL\360 (can only be run on IBM/360 typewriter environment), 5 bytes: '0x0' \$\endgroup\$
    – Adám
    Commented May 2, 2017 at 16:14
  • 4
    \$\begingroup\$ I like that this one disqualifies most golfing languages and encourages people to explore the limits of practical ones. \$\endgroup\$
    – robbie
    Commented May 4, 2017 at 14:31

48 Answers 48

1
2
1
\$\begingroup\$

Red, 26 Bytes

system/view/screens/1/size

Outputs for example:

1920x1080

The code is pretty self explanatory. The 1 refers to the first screen

\$\endgroup\$
1
\$\begingroup\$

Bash + xdotool + tr, 35 bytes

-1 thanks to @manatwork

xdotool getdisplaygeometry|tr \  x

There are two spaces after the \

Gets geometry, then uses tr to replace spaces with x

\$\endgroup\$
1
  • \$\begingroup\$ Is enough to just escape the space, is shorter than quoting it: tr␠\␠␠x. (Used ␠ to mark spaces here just because HTML rendering collapses consecutive whitespaces.) \$\endgroup\$
    – manatwork
    Commented May 4, 2017 at 8:31
1
\$\begingroup\$

Javascript, 42 bytes

console.log((k=screen).width+'x'+k.height)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Hello and welcome to our site! Make sure to include a byte count in your title for code-golf questions. \$\endgroup\$
    – Wheat Wizard
    Commented May 4, 2017 at 5:25
  • \$\begingroup\$ Okay, thank you! Sorry, I didn't know it was a requirement. \$\endgroup\$ Commented May 5, 2017 at 7:55
1
\$\begingroup\$

Python 3, 54 bytes

Python 2, 52 51 bytes

from PIL.ImageGrab import*;print'%dx%d'%grab().size

Saved 2 bytes by executing in Python 2

Saved 1 byte by changing import

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to the site! I don't have PIL installed, so I can't test, but could you just do i.grab().size instead of the string formatting? I don't think it has to be exactly in the WxH format \$\endgroup\$
    – DJMcMayhem
    Commented May 3, 2017 at 21:23
  • \$\begingroup\$ @DJMcMayhem Hello! I thought the same thing, but OP says specific format of [width]x[height]. i.grab().size prints out a tuple as (1920, 1080). Granite that would save me 8 bytes, I'd rather go for accuracy to OPs requirements \$\endgroup\$ Commented May 3, 2017 at 21:26
1
\$\begingroup\$

Batch, 218 208 206 bytes

I can't golf....

@for /f %%# in ('"@wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"')do @set %%#>nul
@echo %CurrentHorizontalResolution%x%CurrentVerticalResolution%
\$\endgroup\$
1
\$\begingroup\$

Batch, 128 125 124 123 bytes

@for /f "tokens=1,2delims=x " %%A in ('wmic path Win32_VideoController get VideoModeDescription^|find "l"')do @echo %%Ax%%B

Basically the same thing as SteveFest's answer, except I grab a slightly shorter chunk of Wim32_VideoController and then I use something vaguely resembling regex to get the one line that contains the data that I want.

I have no idea why the wmic string needs to be inside of double quotes to work I needed the quotes so that I didn't need to escape the pipe, but ^ is shorter than "", and I can't believe that I can't shorten the wmic command at all.

How It Works

Ordinarily, wmic path Win32_VideoController get VideoModeDescription will display something like this:

VideoModeDescription
3440 x 1440 x 4294967296 colors
 

I can use findstr's super rudimentary attempt at regex find to find the letter "l," which only matches the line with the word "colors." From there, that line is delimited on spaces and the letter "x." %%A contains the first token and %%B contains the second token. After that, I just display the values.

\$\endgroup\$
1
  • \$\begingroup\$ Aw.. I've been looking for ways to shorten my code... Nice answer anyways \$\endgroup\$
    – stevefestl
    Commented May 6, 2017 at 4:12
0
\$\begingroup\$

Python 2, 60 bytes

from win32api import*
u=GetSystemMetrics
print u(0),'x',u(1)
\$\endgroup\$
0
\$\begingroup\$

C#, 89 bytes

_=>{var s=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return s.Width+"x"+s.Height;};
\$\endgroup\$
0
\$\begingroup\$

PHP + GTK 2.0, 69 bytes

This whole answer was written based on http://php-gtk.eu/en/code-hints/grabbing-a-screenshot-with-gdk

<?list($W,$H)=Gdk::get_default_root_window()->get_size();echo$W,x,$H;

Basically, fetches the "root" window (entire screen) and gets it's size.


Another way (based on http://www.kksou.com/php-gtk2/sample-codes/get-the-size-of-display-screen.php):

<?$w=(new GtkWindow())->get_screen();echo$w->get_width(),x,$w->get_height();

Gets the screen where the window was created, displaying it's dimentions.

\$\endgroup\$
0
\$\begingroup\$

Javascript, 46 bytes

var s=screen;console.log(s.width+'x'+s.height)

I wanted to write it in the comment but I don't have reputation yet. Can someone explain to me why other solutions don't count bytes for console.log()? If they were counted then this solution would be the shortest.

\$\endgroup\$
0
\$\begingroup\$

MATLAB, 42 byte

a=get(0,'ScreenS');fprintf('%dx%d',a(3:4))

Uses property shortening. Other than that, this is a nice showcase of the inflexibility of MATLAB indexing, requiring a temporary variable a. fprintf prints to stdout by default.

\$\endgroup\$
0
\$\begingroup\$

Clojure, 86 bytes

Shameless port of this Java answer

(#(str(.width %1)"x"(.height %1))(.getScreenSize(java.awt.Toolkit/getDefaultToolkit)))
\$\endgroup\$
0
\$\begingroup\$

8th, 32 bytes

0 hw:displaysize? swap . "x" . .

Returns the device's screen resolution of the first display (0) in pixels in the required format.

Output:

1536x864
\$\endgroup\$
0
\$\begingroup\$

C#, 90 Bytes

()=>{var b=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return b.Width+"x"+b.Height;};

This actually is quite a similiar answer to that one, which already exists, however, I'd like to add a few things. This solution acutally needs a reference to System.Windows.Forms and one to System.Drawing. Those references are not normally added for a console application, so I'm not sure wether it's valid. Therefore I wrote a solution without references, however this has 580 287 Bytes:

using System;using System.Runtime.InteropServices;class P{static void Main()=>Console.Write(G().Item1+"x"+G().Item2);static(int,int)G()=>(GetSystemMetrics(0),GetSystemMetrics(1));[DllImport("User32.dll",ExactSpelling=true,CharSet=CharSet.Auto)]static extern int GetSystemMetrics(int n);}

Here's the solution with line breaks:

using System;
using System.Runtime.InteropServices;

class P
{
    static void Main() => Console.Write(G().Item1 + "x" + G().Item2);
    static (int, int) G() => (GetSystemMetrics(0), GetSystemMetrics(1));

    [DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
    static extern int GetSystemMetrics(int n);
}

This solution was created with the huge help of AdamSchiavone on Stackoverflow.

I also wrote a C# Interactive programm with 123 Bytes, which automatically adds the references, so it can be executed on every machine without problems and without having the reference problems:

#r "System.Windows.Forms"
#r "System.Drawing"
var b=System.Windows.Forms.Screen.PrimaryScreen.Bounds;b.Width+"x"+b.Height

This basically is the same as the first one, but it adds the reference automatically and as it is executed in the interactive, you don't need the return statement, you can simply leave away the semicolon.

\$\endgroup\$
5
  • \$\begingroup\$ Your missing the ; on the end of your func. \$\endgroup\$ Commented May 4, 2017 at 8:23
  • \$\begingroup\$ Side note: Those references aren't added normally for a Console application, they are for a Windows Forms one. Either way they don't need adding into the byte count. \$\endgroup\$ Commented May 4, 2017 at 8:24
  • \$\begingroup\$ @TheLethalCoder You're right, edited my answer. \$\endgroup\$
    – MetaColon
    Commented May 4, 2017 at 12:42
  • 1
    \$\begingroup\$ In your large solution you can change internal static class Program to just class P, you could probably remove static from the methods and use new P(), you can change IntPtr to int I believe, Move the i() out of the struct probably, change the struct to a class and probably a couple of other things. \$\endgroup\$ Commented May 4, 2017 at 12:52
  • \$\begingroup\$ @TheLethalCoder I'll update my answer. Unfortunately, I won't have time to do this till tomorrow. \$\endgroup\$
    – MetaColon
    Commented May 4, 2017 at 13:01
0
\$\begingroup\$

VBA (32-Bit), 110 Bytes

Locally declared windows function and anonymous VBE immediate window function that takes no input and outputs to the VBE Immediate window.

Note: The below may be made into a 64-bit compatible function by the addition of PtrSafe following Declare

Declare Function GetSystemMetrics Lib"user32.dll"(ByVal i&)As Long
?GetSystemMetrics(0)&"x"&GetSystemMetrics(1)
\$\endgroup\$
0
\$\begingroup\$

VBScript, 201 bytes

For Each x in CreateObject("WbemScripting.SWbemLocator").ConnectServer(".","root\cimv2").ExecQuery("SELECT * FROM WIN32_VIDEOCONTROLLER")
a=Split(x.VideoModeDescription," x ")
msgbox a(0)&"x"&a(1)
Next

Connection to Local WMI is eating up all the bytes.....

\$\endgroup\$
0
\$\begingroup\$

VB6, 57 bytes

Sub a:Msgbox Screen.Width/15&"x"&Screen.Height/15:End Sub
\$\endgroup\$
-1
\$\begingroup\$

Bash, 21 bytes

tr , x</*/*/*/fb0/v*

If the device has frame buffer you can query /sys/class/graphics/fb0/virtual_size to get resolution. Width and Height are delimited by , so tr translates to x. Path to the file is shortened by using * to the point there are no ambiguities.

\$\endgroup\$
5
  • \$\begingroup\$ On my machine that glob also matches 4 files + 1 symlink in /usr/share/games/fortunes/vi, so I have to specify …/*0/… instead of …/f*/… (not sure whether you can count on that “0”, I'm not framebuffer fan). \$\endgroup\$
    – manatwork
    Commented May 3, 2017 at 13:22
  • \$\begingroup\$ That depends upon directories. \$\endgroup\$
    – Pandya
    Commented May 3, 2017 at 14:04
  • \$\begingroup\$ several upvoted answers do it as well, edited for a less likely to hapen option \$\endgroup\$
    – marcosm
    Commented May 3, 2017 at 14:10
  • 1
    \$\begingroup\$ Note that this gives the size of the console frame buffer, which for X11 is often quite different from the size of the screen you are sitting in front of when logged in over the network. \$\endgroup\$
    – Jens
    Commented May 3, 2017 at 14:15
  • \$\begingroup\$ console framebuffer is fbcon, not fb0. This outputs resolution of screen connected as framebuffer device fb0. \$\endgroup\$
    – marcosm
    Commented May 3, 2017 at 18:45
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.