Tag Archives: screen

How to get the Computer Screens Resolution using VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 4.20 out of 5)
 

At some point you may need to get a computers monitor/video resolution for sizing your applications and/or your applications controls/fonts or whatever. It is actually easy to do so in ALL versions of Visual Basic. Only a very small amount of additional code is required to get the Resolution in 6.0 compared to doing it in .NET. Below is doing it in VB 6.0.


Visual Basic 6.0

To get the resolution in VB Classic is quite easy. You can use the following code below…

Screen.Width – For the Horizonal Resolution.

Screen.Height – to get the Vertical resolution.

But if you use those codes by themselves, it will not return the resolution in Pixels which is probably the format you are wanting. There is an extra piece of code for getting the resolution in Pixels…

Screen.Width / Screen.TwipsPerPixelX

Screen.Height / Screen.TwipsPerPixelY

Below is the code put together with a Messagebox displaying the resolution for the screen…

'
'Simply throws a messagebox with the current resolution of the screen...
MsgBox("The Screen Resolution is: " & Screen.Width / Screen.TwipsPerPixelX & "x" & Screen.Height / Screen.TwipsPerPixelY)

What about the VB.NET versions? Even easier actually.


Visual Basic.NET & VB 2008/2010, and VB 2013

What we want is the Screen class located at:

System.Windows.Forms.Screen

This handy little Class has data related to the computers display screen. Below is a simply example…

'Simply display a messagebox with the resolution of the computers screen.
MsgBox(Screen.PrimaryScreen.Bounds.Size.ToString)

If a computer has more than one(1) screen/monitor, then you could also use the ‘Screen’ class to get the resolution for that screen.

Example: Say your application was running on a computer with Two(2x) monitors/displays and you wanted to know the resolution of the monitor your Form is shown on or the monitor that is showing the biggest portion of your Form? The Screen Class can help you with that…

'
'If you had multiple displays, and you wanted to know the resolution of the display
' that contains your form, this code would do that for you.
MsgBox(Screen.FromControl(Me).Bounds.Size.ToString)

 

In Visual Basic 2005 and/or Visual Basic 2008, VB 2010, and newer you can even use the MY interface to access the Screen class.

Example…

'
'Use the MY shortcut in VB 05/08 to get the sacreen size...
MsgBox(My.Computer.Screen.Bounds.Size.ToString)

Well, thats all there is to it if you wanted to get the Computers Display/Screen Resolution in your Applications. Its easy to do from Visual Basic 5.0/6.0 through to Visual Basic 2005/2008. Have fun! 🙂

Jason

Easy way to Center the applications Form during Runtime in VB.Net.

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

This is a very simple snippet and will work with all versions of Visual Basic.NET.

IF you want to easily and quickly center your Form in relation to the computer screen, just simply call the Subroutine below.

        '
        'This Sub is available in All versions of VB.NET.
        Me.CenterToScreen()

 

That’s all there is to it. Just simply call that Sub whenever you need or want your application/form to center itself while it is running in runtime mode.

Jason

Easy way to take a screenshot in VB 2008 and Visual Basic.NET 2010

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

The newer 2.0 version of the DotNET Framework added alot of new features. One of them is the ability to natively copy the visual contents of the screen. You can find this Sub() under the: System.Drawing.Graphics namespace named – ‘CopyFromScreen’. The ‘CopyFromScreen’ Sub has 4x overloads. You can specify the Destination coordinates to put the image and the Source coordinates to get the image. Also available is CopyPixelOperation which allows you to select the way you want to copy the pixels. System.Drawing.CopyPixelOperation.SourceCopy is the most common operation. Below is a simple demonstration to take a screenshot in your applications.

This basic sample only needs a picturebox.


'Copy the screen contents to the picturebox control. Select the X and Y positions to get the‘screens image,
'then select the X,Y positions to set the screen image to the picturebox control.
'You then simply specify how much of the image you want to copy. picScreen is the name of the picturebox I used.

picScreen.CreateGraphics.CopyFromScreen(0, 0, 0, 0, New Size(100, 100))

How to get the Computers Monitor Display Resolution using VB 2005 and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

This shows how to use the MY interface to check what the resolution is for the Primary Display. This will return the size in Pixels. Since i’m using the MY interface, the code will only work with VB.NET 2005, VB 2008, and Visual Basic 2010.

 

'Get the displays current Resolution.
MsgBox("Resolution is: " & My.Computer.Screen.Bounds.Width.ToString & "x" _
    & My.Computer.Screen.Bounds.Height.ToString & " Pixels")