/// <summary> /// Capture a region of the screen using Direct3D /// </summary> /// <param name="handle">The handle of a window</param> /// <param name="region">The region to capture (in screen coordinates)</param> /// <returns>A bitmap containing the captured region, this should be disposed of appropriately when finished with it</returns> public static Bitmap CaptureRegionDirect3D(IntPtr handle, Rectangle region) { IntPtr hWnd = handle; Bitmap bitmap = null; // We are only supporting the primary display adapter for Direct3D mode SlimDX.Direct3D9.AdapterInformation adapterInfo = _direct3D9.Adapters.DefaultAdapter; SlimDX.Direct3D9.Device device; #region Get Direct3D Device // Retrieve the existing Direct3D device if we already created one for the given handle if (_direct3DDeviceCache.ContainsKey(hWnd)) { device = _direct3DDeviceCache[hWnd]; } // We need to create a new device else { // Setup the device creation parameters SlimDX.Direct3D9.PresentParameters parameters = new SlimDX.Direct3D9.PresentParameters(); parameters.BackBufferFormat = adapterInfo.CurrentDisplayMode.Format; Rectangle clientRect = NativeMethods.GetAbsoluteClientRect(hWnd); parameters.BackBufferHeight = clientRect.Height; parameters.BackBufferWidth = clientRect.Width; parameters.Multisample = SlimDX.Direct3D9.MultisampleType.None; parameters.SwapEffect = SlimDX.Direct3D9.SwapEffect.Discard; parameters.DeviceWindowHandle = hWnd; parameters.PresentationInterval = SlimDX.Direct3D9.PresentInterval.Default; parameters.FullScreenRefreshRateInHertz = 0; // Create the Direct3D device device = new SlimDX.Direct3D9.Device(_direct3D9, adapterInfo.Adapter, SlimDX.Direct3D9.DeviceType.Hardware, hWnd, SlimDX.Direct3D9.CreateFlags.SoftwareVertexProcessing, parameters); _direct3DDeviceCache.Add(hWnd, device); } #endregion // Capture the screen and copy the region into a Bitmap using ( SlimDX.Direct3D9.Surface surface = SlimDX.Direct3D9.Surface.CreateOffscreenPlain(device, adapterInfo.CurrentDisplayMode.Width, adapterInfo.CurrentDisplayMode.Height, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.SystemMemory)) { device.GetFrontBufferData(0, surface); // Update: thanks digitalutopia1 for pointing out that SlimDX have fixed a bug // where they previously expected a RECT type structure for their Rectangle bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(surface, SlimDX.Direct3D9.ImageFileFormat.Bmp, new Rectangle(region.Left, region.Top, region.Width, region.Height))); // Previous SlimDX bug workaround: new Rectangle(region.Left, region.Top, region.Right, region.Bottom))); } return(bitmap); }
public Direct3D9Graphics(System.Windows.Forms.Control control) { c = control; d3d = new SlimDX.Direct3D9.Direct3D(); SlimDX.Direct3D9.PresentParameters pp = new SlimDX.Direct3D9.PresentParameters(); pp.SwapEffect = SlimDX.Direct3D9.SwapEffect.Discard; pp.DeviceWindowHandle = control.Handle; pp.Windowed = true; pp.BackBufferWidth = control.Width; pp.BackBufferHeight = control.Height; pp.BackBufferFormat = SlimDX.Direct3D9.Format.A8R8G8B8; device = new SlimDX.Direct3D9.Device( d3d, 0, SlimDX.Direct3D9.DeviceType.Hardware, control.Handle, SlimDX.Direct3D9.CreateFlags.HardwareVertexProcessing, pp); line = new SlimDX.Direct3D9.Line(device); fCurrent = new System.Drawing.Font("ARIAL", 10); font = new SlimDX.Direct3D9.Font(device, fCurrent); transform = new TransformationMatrix(); }
/// <summary> /// Capture a region of the screen using Direct3D /// </summary> /// <param name="handle">The handle of a window</param> /// <param name="region">The region to capture (in screen coordinates)</param> /// <returns>A bitmap containing the captured region, this should be disposed of appropriately when finished with it</returns> public static Bitmap CaptureRegionDirect3D(IntPtr handle, Rectangle region) { IntPtr hWnd = handle; Bitmap bitmap = null; // We are only supporting the primary display adapter for Direct3D mode SlimDX.Direct3D9.AdapterInformation adapterInfo = _direct3D9.Adapters.DefaultAdapter; SlimDX.Direct3D9.Device device; #region Get Direct3D Device // Retrieve the existing Direct3D device if we already created one for the given handle if (_direct3DDeviceCache.ContainsKey (hWnd)) { device = _direct3DDeviceCache [hWnd]; } // We need to create a new device else { // Setup the device creation parameters SlimDX.Direct3D9.PresentParameters parameters = new SlimDX.Direct3D9.PresentParameters (); parameters.BackBufferFormat = adapterInfo.CurrentDisplayMode.Format; Rectangle clientRect = NativeMethods.GetAbsoluteClientRect (hWnd); parameters.BackBufferHeight = clientRect.Height; parameters.BackBufferWidth = clientRect.Width; parameters.Multisample = SlimDX.Direct3D9.MultisampleType.None; parameters.SwapEffect = SlimDX.Direct3D9.SwapEffect.Discard; parameters.DeviceWindowHandle = hWnd; parameters.PresentationInterval = SlimDX.Direct3D9.PresentInterval.Default; parameters.FullScreenRefreshRateInHertz = 0; // Create the Direct3D device device = new SlimDX.Direct3D9.Device (_direct3D9, adapterInfo.Adapter, SlimDX.Direct3D9.DeviceType.Hardware, hWnd, SlimDX.Direct3D9.CreateFlags.SoftwareVertexProcessing, parameters); _direct3DDeviceCache.Add (hWnd, device); } #endregion // Capture the screen and copy the region into a Bitmap using (SlimDX.Direct3D9.Surface surface = SlimDX.Direct3D9.Surface.CreateOffscreenPlain (device, adapterInfo.CurrentDisplayMode.Width, adapterInfo.CurrentDisplayMode.Height, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.SystemMemory)) { device.GetFrontBufferData (0, surface); // Update: thanks digitalutopia1 for pointing out that SlimDX have fixed a bug // where they previously expected a RECT type structure for their Rectangle //Rectangle r = new Rectangle (region.Left, region.Top, region.Width, region.Height); bitmap = new Bitmap (SlimDX.Direct3D9.Surface.ToStream (surface, SlimDX.Direct3D9.ImageFileFormat.Bmp)); // Previous SlimDX bug workaround: new Rectangle(region.Left, region.Top, region.Right, region.Bottom))); } return bitmap; }