Monitor a Secondary Screen from a Form - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

I wanted to post this tutorial in case you come across a similar problem:

I connected my computer to my new HDTV through HDMI cable. What I did was I set the TV screen as extended screen. I could have mirrored the TV to my main screen, but that just ruined the screen resolution.

monitor extended screen in visual basic .net

Everything worked good, and I was able to watch movies on my big screen from my computer. The problem was that my computer was in my bedroom, and the TV was in the living room. So it was hard to control the windows that were shown on the TV because I didn't see the cursor on the TV from my bedroom. For example when I finish watching something on YouTube on the big screen, I had to drag the browser to the main computer screen. So I made this program in Visual Basic 2010 so I can be able to see the second screen and the mouse.

I added a PictureBox and a Timer to the form:

monitor hdtv from visual basic .net

I set the PictureBox size mode propery to Zoom, I set the Timer interval to 1, and enabled the timer.

Go to the code page and the following declarations:

Public Class Form1
    Public Shared Image As Image
    Public Shared Bound As Rectangle

Then add the following Function:

Shared Function GetDesktopImage(Optional ByVal Width As Integer = 0, Optional ByVal Height As Integer = 0, Optional ByVal ShowCursor As Boolean = True) As Image
        
        Dim ScreenNumber As Integer = 1  'change the number 1 to your screen number
        Dim W As Integer = Screen.AllScreens(ScreenNumber).Bounds.Width 
        Dim H As Integer = Screen.AllScreens(ScreenNumber).Bounds.Height
        Dim DesktopBitmap As New Bitmap(W, H)
        Dim g As Graphics = Graphics.FromImage(DesktopBitmap)
        g.CopyFromScreen(Screen.PrimaryScreen.Bounds.Width, 0, 0, 0, New Size(Screen.AllScreens(ScreenNumber).Bounds.Width, Screen.AllScreens(ScreenNumber).Bounds.Height), CopyPixelOperation.SourceCopy)
        If ShowCursor Then Cursors.Default.Draw(g, New Rectangle(New Point(Cursor.Position.X - Screen.PrimaryScreen.Bounds.Width, Cursor.Position.Y), New Size(32, 32)))
        g.Dispose()
        If Width = 0 And Height = 0 Then
            Image = DesktopBitmap
            Return DesktopBitmap
 
        Else
            Dim ScaledBitmap As Image = DesktopBitmap.GetThumbnailImage(Width, Height, Nothing, IntPtr.Zero)
            DesktopBitmap.Dispose()
            Image = ScaledBitmap
            Return ScaledBitmap
        End If
    End Function

Then add the following Timer tick handling event:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        PictureBox1.Image = GetDesktopImage()
    End Sub

Run the form, and you should be able to monitor the second screen from you main screen.

display second screen on your main screen in visual basic .net


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.