Réponse acceptée !
Salut, on t'aurait tout aussi bien répondu sur CSharpFR, si la réponse te convient je déplacerais le thread.
[ SuppressUnmanagedCodeSecurity ]
internal static class Win32
{
[ StructLayout( LayoutKind.Sequential ) ]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
//[ DllImport( "user32.dll" ) ]
//private static extern IntPtr GetDesktopWindow( );
[ DllImport( "user32.dll" ) ]
[ return: MarshalAs( UnmanagedType.Bool ) ]
private static extern bool PrintWindow(
IntPtr hWnd, IntPtr hDCBlt, uint nFlags );
[ DllImport( "user32.dll", SetLastError = true ) ]
[ return: MarshalAs( UnmanagedType.Bool ) ]
private static extern bool GetWindowRect(
IntPtr hWnd, out RECT lpRect );
[ DllImport( "user32.dll",
CharSet = CharSet.Unicode, SetLastError = true ) ]
private static extern IntPtr FindWindow(
string lpClassName, string lpWindowName );
[ DllImport( "user32.dll",
CharSet = CharSet.Unicode, SetLastError = true ) ]
private static extern IntPtr FindWindowEx( IntPtr hwndParent,
IntPtr hwndChildAfter, string lpszClass, string lpszWindow );
private static IntPtr FindDesktopWindow( )
{
IntPtr hWnd = FindWindow( "Progman", "Program Manager" );
if ( hWnd != IntPtr.Zero )
{
hWnd = FindWindowEx( hWnd,
IntPtr.Zero, "SHELLDLL_DefView", null );
if ( hWnd != IntPtr.Zero )
{
hWnd = FindWindowEx( hWnd,
IntPtr.Zero, "SysListView32", null );
}
}
return hWnd;
}
public static Bitmap GetTaskBarBitmap( )
{
Bitmap bmp = null;
IntPtr hWnd = FindWindow( "Shell_TrayWnd", null );
if ( hWnd != IntPtr.Zero )
{
RECT rect;
if ( GetWindowRect( hWnd, out rect ) )
{
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
bmp = new Bitmap( width, height,
PixelFormat.Format24bppRgb ); // Verifier Screen BPP.
using ( Graphics g = Graphics.FromImage( bmp ) )
{
IntPtr hDC = g.GetHdc( );
PrintWindow( hWnd, hDC, 0 );
g.ReleaseHdc( hDC );
}
}
}
return bmp;
}
public static Bitmap GetDesktopBitmap( )
{
Bitmap bmp = null;
IntPtr hWnd = FindDesktopWindow( );
if ( hWnd != IntPtr.Zero )
{
RECT rect;
if ( GetWindowRect( hWnd, out rect ) )
{
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
bmp = new Bitmap( width, height,
PixelFormat.Format24bppRgb ); // Verifier Screen BPP.
using ( Graphics g = Graphics.FromImage( bmp ) )
{
IntPtr hDC = g.GetHdc( );
PrintWindow( hWnd, hDC, 0 );
g.ReleaseHdc( hDC );
}
}
}
return bmp;
}
}