SHARE
TWEET

Some C# .NET and DX

a guest Aug 1st, 2011 337 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private static SlimDX.Direct3D9.Direct3D _direct3D9 = new SlimDX.Direct3D9.Direct3D();
  2. private static Dictionary<IntPtr, SlimDX.Direct3D9.Device> _direct3DDeviceCache = new Dictionary<IntPtr, SlimDX.Direct3D9.Device>();
  3.  
  4. private SlimDX.Direct3D9.AdapterCollection _adaptersInfo = _direct3D9.Adapters;
  5.  
  6. --------------------
  7.  
  8. // Executed from Timer
  9. private void DeskCapture()
  10. {
  11.         _GlobalVariables.var_hWnd = User32.GetWindowsHandle();
  12.        
  13.         Bitmap b;
  14.         b = CaptureDXScreen(_GlobalVariables.var_hWnd);
  15.  
  16.         if (true)
  17.                 b.Save(@"_last_scr.jpg", ImageFormat.Jpeg);
  18.        
  19.         b.Dispose();
  20.  
  21. }
  22.  
  23. public static Bitmap CaptureDXScreen(IntPtr hWnd)
  24. {
  25.         Bitmap bitmap = null;
  26.  
  27.         SlimDX.Direct3D9.AdapterInformation adapterInfo = _direct3D9.Adapters[_GlobalVariables.SelectedDisplay];
  28.  
  29.         SlimDX.Direct3D9.Device device;            
  30.  
  31.         int DisplayWidth = adapterInfo.CurrentDisplayMode.Width;
  32.         int DisplayHeight = adapterInfo.CurrentDisplayMode.Height;
  33.  
  34.         #region Get Direct3D Device
  35.  
  36.         // Retrieve the existing Direct3D device if we already created one for the given handle
  37.         if (_direct3DDeviceCache.ContainsKey(hWnd))
  38.         {
  39.                 device = _direct3DDeviceCache[hWnd];
  40.         }
  41.         // We need to create a new device
  42.         else
  43.         {
  44.                 SlimDX.Direct3D9.PresentParameters presentParams = new SlimDX.Direct3D9.PresentParameters();
  45.                
  46.                 presentParams.Windowed = true;
  47.                 presentParams.BackBufferCount = 1;
  48.                 presentParams.BackBufferFormat = adapterInfo.CurrentDisplayMode.Format;
  49.                 presentParams.BackBufferWidth = DisplayWidth;
  50.                 presentParams.BackBufferHeight = DisplayHeight;
  51.                 presentParams.SwapEffect = SlimDX.Direct3D9.SwapEffect.Flip;
  52.                 presentParams.DeviceWindowHandle = hWnd;
  53.                 presentParams.PresentationInterval = SlimDX.Direct3D9.PresentInterval.One;
  54.                 presentParams.FullScreenRefreshRateInHertz = 0;
  55.                 presentParams.PresentFlags = SlimDX.Direct3D9.PresentFlags.LockableBackBuffer; //Massive speed-up?
  56.                 //presentParams.PresentFlags = SlimDX.Direct3D9.PresentFlags.None;
  57.  
  58.                 presentParams.EnableAutoDepthStencil = true;
  59.                 while (true)
  60.                 {
  61.                         presentParams.AutoDepthStencilFormat = SlimDX.Direct3D9.Format.D32;
  62.                         if (_direct3D9.CheckDeviceFormat(0, SlimDX.Direct3D9.DeviceType.Hardware, presentParams.BackBufferFormat, SlimDX.Direct3D9.Usage.DepthStencil, SlimDX.Direct3D9.ResourceType.Surface, presentParams.AutoDepthStencilFormat)) break;
  63.  
  64.                         presentParams.AutoDepthStencilFormat = SlimDX.Direct3D9.Format.D24X8;
  65.                         if (_direct3D9.CheckDeviceFormat(0, SlimDX.Direct3D9.DeviceType.Hardware, presentParams.BackBufferFormat, SlimDX.Direct3D9.Usage.DepthStencil, SlimDX.Direct3D9.ResourceType.Surface, presentParams.AutoDepthStencilFormat)) break;
  66.  
  67.                         presentParams.AutoDepthStencilFormat = SlimDX.Direct3D9.Format.D16;
  68.                         if (_direct3D9.CheckDeviceFormat(0, SlimDX.Direct3D9.DeviceType.Hardware, presentParams.BackBufferFormat, SlimDX.Direct3D9.Usage.DepthStencil, SlimDX.Direct3D9.ResourceType.Surface, presentParams.AutoDepthStencilFormat)) break;
  69.  
  70.                         // Your graphics card does not support 16bit, 24bit nor 32bit depth stencil buffers!
  71.                 }
  72.                
  73.                 device = new SlimDX.Direct3D9.Device(_direct3D9, adapterInfo.Adapter, SlimDX.Direct3D9.DeviceType.Hardware, hWnd, SlimDX.Direct3D9.CreateFlags.HardwareVertexProcessing, presentParams);
  74.                 _direct3DDeviceCache.Add(hWnd, device);
  75.  
  76.         }
  77.         #endregion
  78.  
  79.         // Capture screen and copy the region into a Bitmap
  80.        
  81.         /* /* Method 1: GetFrontBufferData [!WORKS!] [!SLOWLY!]
  82.                 //------------------------------------------------------------
  83.                 //--NOTE--: While DWM is disabled and device.Adapter is >0 (not primary) than picture on grabbed surface is shifted in some direction by the width of primary screen
  84.                 //--------: *When DWM is enabled it works ok
  85.                 //------------------------------------------------------------
  86.         using (SlimDX.Direct3D9.Surface surface = SlimDX.Direct3D9.Surface.CreateOffscreenPlain(device, adapterInfo.CurrentDisplayMode.Width, adapterInfo.CurrentDisplayMode.Height, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.SystemMemory))
  87.         {
  88.                 device.GetFrontBufferData(0, surface);
  89.                 bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(surface, SlimDX.Direct3D9.ImageFileFormat.Bmp, new Rectangle(0, 0, DisplayWidth, DisplayHeight)));
  90.         }
  91.         // */
  92.  
  93.  
  94.         //* /* Method 2: GetBackBuffer [!DOES NOT WORK!] [!FASTER?!]
  95.                 //------------------------------------------------------------
  96.                 //--NOTE--: Output picture is black. DWM no matters I guess...
  97.                 //------------------------------------------------------------
  98.         int _width = DisplayWidth/_GlobalVariables.var_ScreenDivide;
  99.         int _height = DisplayHeight / _GlobalVariables.var_ScreenDivide;
  100.  
  101.         Surface _renderTarget = null;
  102.        
  103.         using (Surface backBuffer = device.GetBackBuffer(0, 0))
  104.         {
  105.                 Surface.ToFile(backBuffer, "__m2_backbuffer.jpg", ImageFileFormat.Jpg);
  106.                 /// First Get Render Target, so we know the exactly size of the surface
  107.                 Surface renderTargetTemp = device.GetRenderTarget(0);
  108.  
  109.                 // Create our Surface  
  110.                 _renderTarget = Surface.CreateRenderTarget(device, _width, _height, renderTargetTemp.Description.Format, MultisampleType.None, 0, false);
  111.                 // Resize from Render Surface to Our Surface || src,dest
  112.                 device.StretchRectangle(renderTargetTemp, _renderTarget, TextureFilter.None);
  113.                 // Get Render Data
  114.                 renderTargetTemp = _renderTarget;
  115.  
  116.                 // Create offscreen surface to use as copy of render target data
  117.                 _renderTarget = Surface.CreateOffscreenPlain(device, _width, _height, renderTargetTemp.Description.Format, Pool.SystemMemory);
  118.  
  119.                 // copies renderTarget data from device memory to system memory || src,dest
  120.                 device.GetRenderTargetData(renderTargetTemp, _renderTarget);
  121.  
  122.                 // And release our surface
  123.                 renderTargetTemp.Dispose();
  124.  
  125.                 //enkel als debug:
  126.                 Surface.ToFile(_renderTarget, "_m2_surface.jpg", ImageFileFormat.Jpg);
  127.  
  128.                 bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(_renderTarget, SlimDX.Direct3D9.ImageFileFormat.Bmp, new Rectangle(0, 0, _width, _height)));
  129.         }
  130.         // */
  131.  
  132.         /* /* Method 3: GetBackBuffer [!DOES NOT WORK!] [!FASTER?!]
  133.                 //------------------------------------------------------------
  134.                 //--NOTE--: Output picture is black. DWM no matters I guess...
  135.                 //------------------------------------------------------------
  136.         using (SlimDX.Direct3D9.Surface surface = device.GetRenderTarget(0))
  137.         {
  138.                 Surface.ToFile(surface, "_m3_ss1.jpg", ImageFileFormat.Jpg);
  139.                 using (SlimDX.Direct3D9.Surface surface2 = SlimDX.Direct3D9.Surface.CreateOffscreenPlain(device, surface.Description.Width, surface.Description.Height, surface.Description.Format, SlimDX.Direct3D9.Pool.SystemMemory))
  140.                 {
  141.                         Surface.ToFile(surface2, "_m3_ss2.jpg", ImageFileFormat.Jpg);
  142.                         device.GetRenderTargetData(surface, surface2);
  143.                         // You have to use client coordinates here
  144.                         bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(surface2, SlimDX.Direct3D9.ImageFileFormat.Bmp, new Rectangle(0, 0, _width, _height)));
  145.                 }
  146.         }
  147.         // */
  148.    
  149.         return bitmap;
  150.  
  151. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top