![]() |
Sylloge
A C# helper library
|
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 using System; 00017 using System.Collections.Generic; 00018 using System.Text; 00019 00020 namespace Sylloge.Device 00021 { 00025 public static class Screen 00026 { 00031 public static System.Drawing.Image CaptureDesktop() 00032 { 00033 return Sylloge.Device.Screen.CaptureObject(Sylloge.WinAPI.User32.GetDesktopWindow()); 00034 } 00035 00041 public static System.Drawing.Image CaptureObject(IntPtr handle) 00042 { 00043 try { 00044 // get te hDC of the target window 00045 IntPtr Source = Sylloge.WinAPI.User32.GetWindowDC(handle); 00046 // get the size 00047 Sylloge.WinAPI.Rectangle WindowRectangle = new Sylloge.WinAPI.Rectangle(); 00048 Sylloge.WinAPI.User32.GetWindowRect(handle, ref WindowRectangle); 00049 int Width = WindowRectangle.Right - WindowRectangle.Left; 00050 int Height = WindowRectangle.Bottom - WindowRectangle.Top; 00051 // create a device context we can copy to 00052 IntPtr Destination = Sylloge.WinAPI.GDI.CreateCompatibleDC(Source); 00053 // create a bitmap we can copy it to, 00054 // using GetDeviceCaps to get the width/height 00055 IntPtr BitmapPtr = Sylloge.WinAPI.GDI.CreateCompatibleBitmap(Source, Width, Height); 00056 // select the bitmap object 00057 IntPtr OldPtr = Sylloge.WinAPI.GDI.SelectObject(Destination, BitmapPtr); 00058 // bitblt over 00059 Sylloge.WinAPI.GDI.BitBlt(Destination, 0, 0, Width, Height, Source, 0, 0, Sylloge.WinAPI.Constants.SRCCOPY); 00060 // restore selection 00061 Sylloge.WinAPI.GDI.SelectObject(Destination, OldPtr); 00062 // clean up 00063 Sylloge.WinAPI.GDI.DeleteDC(Destination); 00064 Sylloge.WinAPI.User32.ReleaseDC(handle, Source); 00065 // get a .NET image object for it 00066 System.Drawing.Image Value = System.Drawing.Image.FromHbitmap(BitmapPtr); 00067 // free up the Bitmap object 00068 Sylloge.WinAPI.GDI.DeleteObject(BitmapPtr); 00069 return Value; 00070 } catch (Exception ex) { 00071 Sylloge.App.Out(ex); 00072 } 00073 return null; 00074 } 00075 } 00076 }
1.7.4