Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using SharpDX;
  9. using SharpDX.DXGI;
  10. using SharpDX.Direct3D11;
  11. using System.Drawing.Imaging;
  12. using System.Windows.Forms;
  13.  
  14. namespace WindowsFormsApp1
  15. {
  16.     class DXCapture
  17.     {
  18.         PictureBox pictureBox;
  19.         bool ready = false;
  20.         bool Recording = false;
  21.         Task RecordingTask;
  22.         int saveSizeX;
  23.         int saveSizeY;
  24.         int width;
  25.         int height;
  26.         int numAdapter;
  27.         int numOutput;
  28.         Factory1 factory;
  29.         Adapter1 adapter;
  30.         SharpDX.Direct3D11.Device device;
  31.         Output output;
  32.         Output1 output1;
  33.         Texture2DDescription texture2DDescription;
  34.         Texture2D screenTexture;
  35.         OutputDuplication duplicatedOutput;
  36.         SharpDX.DXGI.Resource screenResource;
  37.  
  38.  
  39.         public DXCapture( int sizeX = 1920, int sizeY = 1080, int numAdapter = 0, int numOutput = 0)
  40.         {
  41.             this.pictureBox = Form1.pictureBox1;
  42.             this.saveSizeX = sizeX;
  43.             this.saveSizeY = sizeY;
  44.             this.numAdapter = numAdapter;
  45.             this.numOutput = numOutput;
  46.             InitDX();
  47.         }
  48.  
  49.         public void InitDX()
  50.         {
  51.             try
  52.             {
  53.                 factory = new SharpDX.DXGI.Factory1();
  54.                 adapter = factory.GetAdapter1(numAdapter);
  55.                 device = new SharpDX.Direct3D11.Device(adapter);
  56.                 output = adapter.GetOutput(numOutput);
  57.                 output1 = output.QueryInterface<Output1>();
  58.  
  59.                
  60.                 width = output.Description.DesktopBounds.Left + output.Description.DesktopBounds.Right;
  61.                 height = output.Description.DesktopBounds.Top + output.Description.DesktopBounds.Bottom;
  62.  
  63.                 texture2DDescription = new Texture2DDescription
  64.                 {
  65.                     CpuAccessFlags = CpuAccessFlags.Read,
  66.                     BindFlags = BindFlags.None,
  67.                     Format = Format.B8G8R8A8_UNorm,
  68.                     Width = width,
  69.                     Height = height,
  70.                     OptionFlags = ResourceOptionFlags.None,
  71.                     MipLevels = 1,
  72.                     ArraySize = 1,
  73.                     SampleDescription = { Count = 1, Quality = 0 },
  74.                     Usage = ResourceUsage.Staging
  75.                 };
  76.  
  77.                 screenTexture = new Texture2D(device, texture2DDescription);
  78.                 duplicatedOutput = output1.DuplicateOutput(device);
  79.                 screenResource = null;
  80.                 ready = true;
  81.             }
  82.             catch (SharpDXException e)
  83.             {
  84.                 Console.WriteLine("Error InitDX");
  85.             }
  86.         }
  87.  
  88.         public void DisposeDX()
  89.         {
  90.             factory.Dispose();
  91.             factory = null;
  92.             adapter.Dispose();
  93.             adapter = null;
  94.             device.Dispose();
  95.             device = null;
  96.             output.Dispose();
  97.             output = null;
  98.             output1.Dispose();
  99.             output1 = null;
  100.             screenTexture.Dispose();
  101.             screenTexture = null;    
  102.             duplicatedOutput.Dispose();
  103.             duplicatedOutput = null;
  104.             GC.Collect();
  105.         }
  106.  
  107.         private void GetShot()
  108.         {
  109.             try
  110.             {
  111.                 OutputDuplicateFrameInformation duplicateFrameInformation;
  112.                 SharpDX.DXGI.Resource screenResource = null;
  113.                 // Try to get duplicated frame within given time
  114.                
  115.                         duplicatedOutput.AcquireNextFrame(1000, out duplicateFrameInformation, out screenResource);
  116.  
  117.  
  118.                 Task.Factory.StartNew(() =>
  119.                 {
  120.  
  121.                     // copy resource into memory that can be accessed by the CPU
  122.                     using (var screenTexture2D = screenResource.QueryInterface<Texture2D>())
  123.                 {
  124.                     device.ImmediateContext.CopyResource(screenTexture2D, screenTexture);
  125.                 }
  126.  
  127.                 // Get the desktop capture texture
  128.                 var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None);
  129.                 var boundsRect = new System.Drawing.Rectangle(0, 0, width, height);
  130.  
  131.                
  132.                     // Create Drawing.Bitmap
  133.                     using (var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
  134.                     {
  135.                         // Copy pixels from screen capture Texture to GDI bitmap
  136.                         var bitmapData = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat);
  137.                         var sourcePtr = mapSource.DataPointer;
  138.                         var destinationPtr = bitmapData.Scan0;
  139.                         for (int y = 0; y < height; y++)
  140.                         {
  141.                             // Copy a single line
  142.                             Utilities.CopyMemory(destinationPtr, sourcePtr, width * 4);
  143.  
  144.                             // Advance pointers
  145.                             sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch);
  146.                             destinationPtr = IntPtr.Add(destinationPtr, bitmapData.Stride);
  147.                         }
  148.  
  149.                         // Release source and dest locks
  150.                         bitmap.UnlockBits(bitmapData);
  151.  
  152.                         device.ImmediateContext.UnmapSubresource(screenTexture, 0);
  153.  
  154.                         // instant preview in picture box
  155.                         if (pictureBox != null)
  156.                         {
  157.                             pictureBox.Invoke(new Action(() =>
  158.                             {
  159.                                 pictureBox.Image = (System.Drawing.Bitmap)bitmap.Clone();
  160.                             }));
  161.                         }
  162.  
  163.                        
  164.                     }
  165.                 });
  166.  
  167.  
  168.             }
  169.             catch (SharpDXException e)
  170.             {
  171.                 if (e.ResultCode.Code == SharpDX.DXGI.ResultCode.AccessLost.Result.Code)
  172.                 {
  173.                     Console.WriteLine("Error GetShot ACCESS LOST - relaunch in 2s !");
  174.                     Thread.Sleep(2000);
  175.                     DisposeDX();
  176.                     GC.Collect();
  177.                     InitDX();
  178.                 }
  179.                 else if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code)
  180.                 {
  181.                     Console.WriteLine("Error GetShot");
  182.                     throw;
  183.                 }*/
  184.             }
  185.             finally
  186.             {
  187.                 try
  188.                 {
  189.                    
  190.                     // Dispose manually
  191.                     if (screenResource != null)
  192.                     {
  193.                         screenResource.Dispose();
  194.                         screenResource = null;
  195.                         duplicatedOutput.ReleaseFrame();
  196.  
  197.                     }
  198.  
  199.                     // force the Garbage Collector to cleanup memory to prevent memory leaks
  200.                     Task.Factory.StartNew(() => { GC.Collect(); });
  201.                  
  202.                 }
  203.                 catch (Exception e)
  204.                 {
  205.                     Console.WriteLine("Error GetShot finnaly - relaunch in 2s !");
  206.                     Thread.Sleep(2000);
  207.                     DisposeDX();
  208.                     GC.Collect();
  209.                     InitDX();
  210.                 }
  211.                 */
  212.             }
  213.         }
  214.  
  215.         public void ScreenShot()
  216.         {
  217.             if (ready)
  218.             {
  219.                 GetShot();
  220.             }
  221.         }
  222.  
  223.        
  224.  
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement