Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace bot
  8. {
  9.     class DXCapture
  10.     {
  11.         private static SlimDX.Direct3D9.Direct3D _direct3D9 = new SlimDX.Direct3D9.Direct3D();
  12.         private static Dictionary<IntPtr, SlimDX.Direct3D9.Device> _direct3DDeviceCache = new Dictionary<IntPtr, SlimDX.Direct3D9.Device>();
  13.  
  14.         /// Захват скриншота окна hWnd
  15.         public static Bitmap CaptureWindow(IntPtr hWnd)
  16.         {
  17.             Rectangle rect = NativeMethods.GetAbsoluteClientRect(hWnd);
  18.             return CaptureRegionDirect3D(hWnd, rect);
  19.         }
  20.  
  21.         // Захват региона окна
  22.         //  hWnd - указатель на окно
  23.         //  region - захватываемая область
  24.         public static Bitmap CaptureRegionDirect3D(IntPtr handle, Rectangle region)
  25.         {
  26.             IntPtr hWnd = handle;
  27.             Bitmap bitmap = null;
  28.  
  29.             Rectangle r = NativeMethods.GetAbsoluteClientRect(hWnd);
  30.             WinAPI.SetWindowPos(hWnd, hWnd, 0, 0, r.Width, r.Height, Constants.API_SHOWWINDOW);
  31.             WinAPI.SetForegroundWindow(hWnd);
  32.             WinAPI.SetFocus(hWnd);
  33.  
  34.             // We are only supporting the primary display adapter for Direct3D mode
  35.             SlimDX.Direct3D9.AdapterInformation adapterInfo = _direct3D9.Adapters.DefaultAdapter;
  36.             SlimDX.Direct3D9.Device device;
  37.  
  38.             #region Get Direct3D Device
  39.             // Retrieve the existing Direct3D device if we already created one for the given handle
  40.             if (_direct3DDeviceCache.ContainsKey(hWnd))
  41.             {
  42.                 device = _direct3DDeviceCache[hWnd];
  43.             }
  44.             // We need to create a new device
  45.             else
  46.             {
  47.                 // Setup the device creation parameters
  48.                 SlimDX.Direct3D9.PresentParameters parameters = new SlimDX.Direct3D9.PresentParameters();
  49.                 parameters.BackBufferFormat = adapterInfo.CurrentDisplayMode.Format;
  50.                 Rectangle clientRect = NativeMethods.GetAbsoluteClientRect(hWnd);
  51.                 parameters.BackBufferHeight = clientRect.Height;
  52.                 parameters.BackBufferWidth = clientRect.Width;
  53.                 parameters.Multisample = SlimDX.Direct3D9.MultisampleType.None;
  54.                 parameters.SwapEffect = SlimDX.Direct3D9.SwapEffect.Discard;
  55.                 parameters.DeviceWindowHandle = hWnd;
  56.                 parameters.PresentationInterval = SlimDX.Direct3D9.PresentInterval.Default;
  57.                 parameters.FullScreenRefreshRateInHertz = 0;
  58.  
  59.                 // Create the Direct3D device
  60.                 device = new SlimDX.Direct3D9.Device(_direct3D9, adapterInfo.Adapter, SlimDX.Direct3D9.DeviceType.Hardware, hWnd, SlimDX.Direct3D9.CreateFlags.SoftwareVertexProcessing, parameters);
  61.                 _direct3DDeviceCache.Add(hWnd, device);
  62.             }
  63.             #endregion
  64.  
  65.             // Capture the screen and copy the region into a Bitmap
  66.             using (SlimDX.Direct3D9.Surface surface = SlimDX.Direct3D9.Surface.CreateOffscreenPlain(device, adapterInfo.CurrentDisplayMode.Width, adapterInfo.CurrentDisplayMode.Height, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.SystemMemory))
  67.             {
  68.                 device.GetFrontBufferData(0, surface);
  69.  
  70.                 // Update: thanks digitalutopia1 for pointing out that SlimDX have fixed a bug
  71.                 // where they previously expected a RECT type structure for their Rectangle
  72.                 bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(surface, SlimDX.Direct3D9.ImageFileFormat.Bmp, new Rectangle(region.Left, region.Top, region.Width, region.Height)));
  73.                 // Previous SlimDX bug workaround: new Rectangle(region.Left, region.Top, region.Right, region.Bottom)));
  74.  
  75.             }
  76.  
  77.             return bitmap;
  78.         }
  79.     }
  80.  
  81.     /// Обёртки к WIN32 Api
  82.     #region Native Win32 Interop
  83.     [Serializable, StructLayout(LayoutKind.Sequential)]
  84.     internal struct RECT
  85.     {
  86.         public int Left;
  87.         public int Top;
  88.         public int Right;
  89.         public int Bottom;
  90.  
  91.         public RECT(int left, int top, int right, int bottom)
  92.         {
  93.             this.Left = left;
  94.             this.Top = top;
  95.             this.Right = right;
  96.             this.Bottom = bottom;
  97.         }
  98.  
  99.         public Rectangle AsRectangle
  100.         {
  101.             get
  102.             {
  103.                 return new Rectangle(this.Left, this.Top, this.Right - this.Left, this.Bottom - this.Top);
  104.             }
  105.         }
  106.  
  107.         public static RECT FromXYWH(int x, int y, int width, int height)
  108.         {
  109.             return new RECT(x, y, x + width, y + height);
  110.         }
  111.  
  112.         public static RECT FromRectangle(Rectangle rect)
  113.         {
  114.             return new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom);
  115.         }
  116.     }
  117.  
  118.     [System.Security.SuppressUnmanagedCodeSecurity()]
  119.     internal sealed class NativeMethods
  120.     {
  121.  
  122.         /// &lt;summary&gt;
  123.         /// Get a windows client rectangle in a .NET structure
  124.         /// &lt;/summary&gt;
  125.         /// &lt;param name=&quot;hwnd&quot;&gt;The window handle to look up&lt;/param&gt;
  126.         /// &lt;returns&gt;The rectangle&lt;/returns&gt;
  127.         internal static Rectangle GetClientRect(IntPtr hwnd)
  128.         {
  129.             RECT rect = new RECT();
  130.             WinAPI.GetClientRect(hwnd, out rect);
  131.             return rect.AsRectangle;
  132.         }
  133.  
  134.         /// &lt;summary&gt;
  135.         /// Get a windows rectangle in a .NET structure
  136.         /// &lt;/summary&gt;
  137.         /// &lt;param name=&quot;hwnd&quot;&gt;The window handle to look up&lt;/param&gt;
  138.         /// &lt;returns&gt;The rectangle&lt;/returns&gt;
  139.         internal static Rectangle GetWindowRect(IntPtr hwnd)
  140.         {
  141.             RECT rect = new RECT();
  142.             WinAPI.GetWindowRect(hwnd, out rect);
  143.             return rect.AsRectangle;
  144.         }
  145.  
  146.         internal static Rectangle GetAbsoluteClientRect(IntPtr hWnd)
  147.         {
  148.             Rectangle windowRect = NativeMethods.GetWindowRect(hWnd);
  149.             Rectangle clientRect = NativeMethods.GetClientRect(hWnd);
  150.  
  151.             // This gives us the width of the left, right and bottom chrome - we can then determine the top height
  152.             int chromeWidth = (int)((windowRect.Width - clientRect.Width) / 2);
  153.  
  154.             return new Rectangle(new Point(windowRect.X + chromeWidth, windowRect.Y + (windowRect.Height - clientRect.Height - chromeWidth)), clientRect.Size);
  155.         }
  156.  
  157.         internal static void MakeWindowActive(IntPtr hWnd)
  158.         {
  159.  
  160.         }
  161.     }
  162.     #endregion
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement