宣言
Declare Function BitBlt Lib “GDI32.DLL” ( _
ByVal hdcDest As IntPtr, _
ByVal nXDest As Integer, _
ByVal nYDest As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hdcSrc As IntPtr, _
ByVal nXSrc As Integer, _
ByVal nYSrc As Integer, _
ByVal dwRop As TernaryRasterOperations) As Boolean
サンプルのソースです。
準備としては、フォームにボタンを2つと、ピクチャーボックスを1つ貼り付けて下さい。
ピクチャーボックスは、ある程度の大きさがあった方が分かりやすいです。
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class Form1
Region “declare”
Const SRCCOPY As Integer = 13369376
Const CAPTUREBLT As Integer = 1073741824
Private Shared Function GetDC(ByVal hwnd As IntPtr) As IntPtr
End Function
Private Shared Function BitBlt(ByVal hDestDC As IntPtr,
ByVal x As Integer, ByVal y As Integer,
ByVal nWidth As Integer, ByVal nHeight As Integer,
ByVal hSrcDC As IntPtr,
ByVal xSrc As Integer, ByVal ySrc As Integer,
ByVal dwRop As Integer) As Integer
End Function
Private Shared Function ReleaseDC(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr
End Function
Private Shared Function GetWindowDC(ByVal hwnd As IntPtr) As IntPtr
End Function
Private Shared Function GetForegroundWindow() As IntPtr
End Function
Private Shared Function GetWindowRect(ByVal hwnd As IntPtr, ByRef lpRect As RECT) As Integer
End Function
Private Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Structure
End Region
Private Function Cat_Screen() As Bitmap
Dim disDC As IntPtr = GetDC(IntPtr.Zero)
Dim bmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim hDC As IntPtr = g.GetHdc()
BitBlt(hDC, 0, 0, bmp.Width, bmp.Height, disDC, 0, 0, SRCCOPY)
g.ReleaseHdc(hDC)
g.Dispose()
ReleaseDC(IntPtr.Zero, disDC)
Return bmp
End Function
Private Function Cap_ActiveWindow() As Bitmap
Dim hWnd As IntPtr = GetForegroundWindow()
Dim winDC As IntPtr = GetWindowDC(hWnd)
Dim winRect As New RECT
GetWindowRect(hWnd, winRect)
Dim bmp As New Bitmap(winRect.right – winRect.left, winRect.bottom – winRect.top)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim hDC As IntPtr = g.GetHdc()
BitBlt(hDC, 0, 0, bmp.Width, bmp.Height, winDC, 0, 0, SRCCOPY)
g.ReleaseHdc(hDC)
g.Dispose()
ReleaseDC(hWnd, winDC)
Return bmp
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim bmp As Bitmap = Cat_Screen()
PictureBox1.BackgroundImage = bmp
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim bmp As Bitmap = Cap_ActiveWindow()
PictureBox1.BackgroundImage = bmp
End Sub
End Class
ボタンをクリックする事で、画面のキャプチャが取得でき、ピクチャーボックス内に表示されます。
参考情報
※ページのURLは変更になりますので、その際は検索結果を開いて下さい。
システム開発のためのVB.NETプログラミング関係一覧に戻る