title: How to capture screenshot without user32.dll which is unmanaged code
body: <p><strong>Overview</strong>
  I am trying to capture a screenshot of external application and load the image to a .NET PictureBox.</p>
  
  <p><strong>user32.dll Unmanaged Code</strong>
  There are a bunch of examples using user32.dll an I am looking for a .NET answer that does not use the user32.dll file.</p>
  
  <p>App #1
  I have provided sample code below that I am using for demonstration purposes. When the program is executed, it parses Processes and saves ones with valid window titles to the drop-down list.</p>
  
  <p>When you select the app name from the drop-down list and click the button, it will try to capture the screenshot app and load into the .NET PictureBox.</p>
  
  <p><img src="https://i.stack.imgur.com/s9rBV.png" alt="App #1"> <img src="https://i.stack.imgur.com/7wDAs.png" alt="App #2"></p>
  
  <p>Form1.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>    using System;
      using System.Collections.Generic;
      using System.Diagnostics;
      using System.Drawing;
      using System.Windows.Forms;
  
      namespace WindowsFormsApplication3
      {
          public partial class Form1 : Form
          {
  
              Dictionary&lt;string, IntPtr&gt; apps = new Dictionary&lt;string, IntPtr&gt;();
  
              public Form1()
              {
                  InitializeComponent();
  
                  foreach (Process process in Process.GetProcesses())
                  {
                      if (string.IsNullOrEmpty(process.MainWindowTitle))
                          continue;
  
                      apps.Add(process.MainWindowTitle, process.MainWindowHandle);
                      comboBox1.Items.Add(process.MainWindowTitle);
                  }
              }
  
              private void button1_Click(object sender, EventArgs e)
              {
                  IntPtr intptr = apps[comboBox1.Items[comboBox1.SelectedIndex].ToString()];
  
                  Graphics g = Graphics.FromHwnd(intptr);
  
                  // get width and height of app #2
                  int width = (int)g.VisibleClipBounds.Width;
                  int height = (int)g.VisibleClipBounds.Height;
  
                  Size s = new Size(width, height);
  
                  // create new bitmap
                  Bitmap wincapture = new Bitmap(width, height, g);
  
                  // code tried.
                  //g.CopyFromScreen(0, 0, 0, 0, new Size(width, height));
                  //g.DrawImage(wincapture, 0, 0, width, height);
  
                  // once App #2 has been captured, show image in picture box.
                  pictureBox1.Image = wincapture;
              }
          }
      }
  </code></pre>
  
  <p>Form1.Designer.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>    namespace WindowsFormsApplication3
      {
          partial class Form1
          {
              /// &lt;summary&gt;
              /// Required designer variable.
              /// &lt;/summary&gt;
              private System.ComponentModel.IContainer components = null;
  
              /// &lt;summary&gt;
              /// Clean up any resources being used.
              /// &lt;/summary&gt;
              /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
              protected override void Dispose(bool disposing)
              {
                  if (disposing &amp;&amp; (components != null))
                  {
                      components.Dispose();
                  }
                  base.Dispose(disposing);
              }
  
              #region Windows Form Designer generated code
  
              /// &lt;summary&gt;
              /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              /// &lt;/summary&gt;
              private void InitializeComponent()
              {
                  this.pictureBox1 = new System.Windows.Forms.PictureBox();
                  this.button1 = new System.Windows.Forms.Button();
                  this.comboBox1 = new System.Windows.Forms.ComboBox();
                  ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
                  this.SuspendLayout();
                  // 
                  // pictureBox1
                  // 
                  this.pictureBox1.Location = new System.Drawing.Point(13, 13);
                  this.pictureBox1.Name = "pictureBox1";
                  this.pictureBox1.Size = new System.Drawing.Size(267, 183);
                  this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                  this.pictureBox1.TabIndex = 0;
                  this.pictureBox1.TabStop = false;
                  // 
                  // button1
                  // 
                  this.button1.Location = new System.Drawing.Point(102, 227);
                  this.button1.Name = "button1";
                  this.button1.Size = new System.Drawing.Size(82, 23);
                  this.button1.TabIndex = 2;
                  this.button1.Text = "View Ext App";
                  this.button1.UseVisualStyleBackColor = true;
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  // 
                  // comboBox1
                  // 
                  this.comboBox1.FormattingEnabled = true;
                  this.comboBox1.Location = new System.Drawing.Point(13, 202);
                  this.comboBox1.Name = "comboBox1";
                  this.comboBox1.Size = new System.Drawing.Size(267, 21);
                  this.comboBox1.TabIndex = 3;
                  // 
                  // Form1
                  // 
                  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                  this.ClientSize = new System.Drawing.Size(292, 256);
                  this.Controls.Add(this.comboBox1);
                  this.Controls.Add(this.button1);
                  this.Controls.Add(this.pictureBox1);
                  this.Name = "Form1";
                  this.Text = "App #1";
                  ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
                  this.ResumeLayout(false);
  
              }
  
              #endregion
  
              private System.Windows.Forms.PictureBox pictureBox1;
              private System.Windows.Forms.Button button1;
              private System.Windows.Forms.ComboBox comboBox1;
          }
      }
  </code></pre>