2013-12-10 1 views
3

Я пытаюсь захватить все экраны на компьютере, я пытался возиться с Screen.AllScreens, а также с VirtualScreens, что я не могу вспомнить, поэтому я переместился на PrimaryScreen, чтобы убедиться, что все остальное работает правильно.VB.NET - Сделайте снимок экрана обо всех экранах на компьютере

Вот мой текущий класс:

Public Class wmCapture 

    Public Shared Function screenCapture() 
     Dim userName As String = Environment.UserName 
     Dim savePath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 
     Dim dateString As String = Date.Now.ToString("yyyyMMddHHmmss") 
     Dim captureSavePath As String = String.Format("{0}\WM\{1}\capture_{2}.png", savePath, userName, dateString) 
     Dim bmp As Bitmap = New Bitmap(_ 
          Screen.PrimaryScreen.Bounds.Width, _ 
          Screen.PrimaryScreen.Bounds.Height) 

     Dim gfx As Graphics = Graphics.FromImage(bmp) 

     gfx.CopyFromScreen(_ 
      Screen.PrimaryScreen.Bounds.Location, _ 
      New Point(0, 0), Screen.PrimaryScreen.Bounds.Size) 

     bmp.Save(captureSavePath) 

    End Function 

End Class 

Что я должен использовать в пространстве имен экрана, чтобы включить все активные экраны?

ответ

4

Вы были близки. Я сделал пару корректировок и могу подтвердить, что он работает с моей стороны.

Public Shared Sub screenCapture() 
    Dim userName As String = Environment.UserName 
    Dim savePath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 
    Dim dateString As String = Date.Now.ToString("yyyyMMddHHmmss") 
    Dim captureSavePath As String = String.Format("{0}\WM\{1}\capture_{2}.png", savePath, userName, dateString) 
    ' This line is modified for multiple screens, also takes into account different screen size (if any) 
    Dim bmp As Bitmap = New Bitmap(_ 
         Screen.AllScreens.Sum(Function(s As Screen) s.Bounds.Width), 
         Screen.AllScreens.Max(Function(s As Screen) s.Bounds.Height)) 
    Dim gfx As Graphics = Graphics.FromImage(bmp) 
    ' This line is modified to take everything based on the size of the bitmap 
    gfx.CopyFromScreen(SystemInformation.VirtualScreen.X, 
         SystemInformation.VirtualScreen.Y, 
         0, 0, SystemInformation.VirtualScreen.Size) 
    ' Oh, create the directory if it doesn't exist 
    Directory.CreateDirectory(Path.GetDirectoryName(captureSavePath)) 
    bmp.Save(captureSavePath) 
End Sub 
+0

Большое спасибо Dan, поэтому я должен использовать Sub вместо функции? Я ссылаюсь на функцию и класс в моем main.vb (form1) – Sam

+0

Также у меня есть проверка каталога в другом классе как часть событий загрузки приложения, но спасибо за добавление ваших :) – Sam

+0

Если метод не имеет типа возврата , используйте 'Sub'. В вашем случае метод ничего не возвращает. – djv

Смежные вопросы