title: Saving Surface to Bitmap and optimizing DirectX screen capture in C#
body: <p>after a whole day of testing I came up with this code, which captures current screen using DirectX (SlimDX) and saves it into a file:</p>
  
  <pre><code>Device d;
  
  public DxScreenCapture()
  {
      PresentParameters present_params = new PresentParameters();
      present_params.Windowed = true;
      present_params.SwapEffect = SwapEffect.Discard;
      d = new Device(new Direct3D(), 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.SoftwareVertexProcessing, present_params);
  }
  
  public Surface CaptureScreen()
  {
      Surface s = Surface.CreateOffscreenPlain(d, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, Format.A8R8G8B8, Pool.Scratch);
      d.GetFrontBufferData(0, s);
      return s;
  }
  </code></pre>
  
  <p>Then I do the following:</p>
  
  <pre><code>   DxScreenCapture sc = new DxScreenCapture();
  </code></pre>
  
  <p>..code here</p>
  
  <pre><code>    private void button1_Click(object sender, EventArgs e)
      {
  
          Stopwatch stopwatch = new Stopwatch();
  
          // Begin timing
          stopwatch.Start();
  
          Surface s = sc.CaptureScreen();
          Surface.ToFile(s, @"c:\temp\test.png", ImageFileFormat.Png);
  
          s.Dispose();
  
          stopwatch.Stop();
  
          textBox1.Text = ("Elapsed:" + stopwatch.Elapsed.TotalMilliseconds);
      }
  </code></pre>
  
  <p><strong>The results are:</strong></p>
  
  <p><strong>0. when I don't save surface:</strong> avg. elapsed time: 80-90ms</p>
  
  <p><strong>1. when I also save Surface to BMP file:</strong> format: ImageFileFormat.Bmp , avg. elapsed time: 120ms, file size: 7mb</p>
  
  <p><strong>2. when I also save Surface to PNG file:</strong> format: ImageFileFormat.Png , avg. elapsed time: 800ms, file size: 300kb</p>
  
  <p><strong>The questions are:</strong></p>
  
  <p><strong>1.</strong> Is it possible to optimise current image capture? According to this article - Directx screen capture should be faster than GDI. For me, GDI usually takes 20ms to get a "Bitmap", whereas it takes 80ms to get "Surfare" using DX (both without saving).</p>
  
  <p><a href="http://www.codeproject.com/Articles/274461/Very-fast-screen-capture-using-DirectX-in-Csharp" rel="noreferrer">http://www.codeproject.com/Articles/274461/Very-fast-screen-capture-using-DirectX-in-Csharp</a></p>
  
  <p><strong>2a.</strong> How to save Surface to PNG image format faster? When I save surface to 7mb BMP file it takes almost 6 times less time, than when I save the same surface to 300kb PNG file..</p>
  
  <p><strong>2b.</strong> Is it possible to save Surface directly to Bitmap so I don't have to create temporary files?</p>
  
  <p>So I don't have to do following: <strong>Surface -> image file; image file open -> bitmap;</strong>, but instead: <strong>Surface -> bitmap</strong></p>
  
  <p>that's all for now. I'll gladly accept any tips, thanks!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Just solved 2b by doing:</p>
  
  <pre><code>Bitmap bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(s, SlimDX.Direct3D9.ImageFileFormat.Bmp));
  </code></pre>
  
  <p><strong>Edit2:</strong></p>
  
  <pre><code>Surface.ToFile(s, @"C:\temp\test.bmp", ImageFileFormat.Bmp);
  Bitmap bitmap = new Bitmap(@"C:\temp\test.bmp");
  </code></pre>
  
  <p>is faster than:</p>
  
  <pre><code>Bitmap bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(s, SlimDX.Direct3D9.ImageFileFormat.Bmp));
  </code></pre>
  
  <p>by 100 ms!!! Yeah, I couldn't believe my eyes too ;) I don't like the idea of temporary file creation, but a 50% performance increase (100-200ms instead of 200-300+) is a very good thing.</p>