Here is some code that will do it.
[C#]
internal class NativeMethods
{
[DllImport('user32.dll')]
public extern static IntPtr GetDesktopWindow();
[System.Runtime.InteropServices.DllImport('user32.dll')]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport('gdi32.dll')]
public static extern UInt64 BitBlt
(IntPtr hDestDC,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hSrcDC,
int xSrc,
int ySrc,
System.Int32 dwRop);
}
// Save the screen capture into a jpg
public void SaveScreen()
{
Image myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics gr1 = Graphics.FromImage(myImage);
IntPtr dc1 = gr1.GetHdc();
IntPtr dc2 = NativeMethods.GetWindowDC(NativeMethods.GetDesktopWindow());
NativeMethods.BitBlt(dc1, 0, 0, Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height, dc2, 0, 0, 13369376);
gr1.ReleaseHdc(dc1);
myImage.Save('screenshot.jpg', ImageFormat.Jpeg);
}
[VB.Net]
Friend Class NativeMethods
_
Public Shared Function GetDesktopWindow() As IntPtr
End Function
_
Public Shared Function GetWindowDC(ByVal hwnd As IntPtr) As IntPtr
End Function
_
Public 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 System.Int32) As UInt64
End Function
End Class ’NativeMethods
’Save the screen capture into a jpg
Private Sub SaveScreen()
Dim myImage = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim gr1 As Graphics = Graphics.FromImage(myImage)
Dim dc1 As IntPtr = gr1.GetHdc()
Dim dc2 As IntPtr = NativeMethods.GetWindowDC(NativeMethods.GetDesktopWindow())
NativeMethods.BitBlt(dc1, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc2, 0, 0, 13369376)
gr1.ReleaseHdc(dc1)
myImage.Save('screenshot.jpg', ImageFormat.Jpeg)
End Sub ’SaveScreen
Share with