VB.Net and C# Functions to Get Bitmaps of All Screens


I was answering a question on the MSDN forums and had written up a simple method to return a list of bitmaps for each screen in use by Windows (e.g. dual monitors or more). In this example, I’m not using the Windows API’s like I have in the past (with BitBlt). Instead, this is done with all managed code:

VB.Net

''' <summary>
''' Returns a list of bitmaps that contain a bitmap for every display screen.
''' </summary>
Public Function ScreenBitmaps() As List(Of Bitmap)
    Dim bmpList As New List(Of Bitmap)

    For Each sc As Screen In Screen.AllScreens
        Dim g As Graphics
        Dim bmp As Bitmap = New Bitmap(sc.Bounds.Width, sc.Bounds.Height)
        g = Graphics.FromImage(bmp)
        g.CopyFromScreen(sc.Bounds.Left, sc.Bounds.Top, 0, 0, New Size(sc.Bounds.Width, sc.Bounds.Height))
        bmpList.Add(bmp)
        g.Dispose() : g = Nothing
    Next

    Return bmpList
End Function

C#

/// <summary>
/// Returns a list of bitmaps that contain a bitmap for every display screen.
/// </summary>
public List<Bitmap> ScreenBitmaps()
{
    List<Bitmap> bmpList = new List<Bitmap>();

    foreach (Screen sc in Screen.AllScreens)
    {
        var bmp = new Bitmap(sc.Bounds.Width, sc.Bounds.Height);

        using (var g = Graphics.FromImage(bmp))
        {
            g.CopyFromScreen(sc.Bounds.Left, sc.Bounds.Top, 0, 0, new Size(sc.Bounds.Width, sc.Bounds.Height));
            bmpList.Add(bmp);
        }
    }

    return bmpList;
}