Csharp/CSharp Tutorial/GUI Windows Forms/Screen capture
Screen capture
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; public class ScreenCaptureByCopyFromScreen : Form {
public ScreenCaptureByCopyFromScreen() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.cmdCapture = new System.Windows.Forms.Button(); ((System.ruponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.pictureBox1.Location = new System.Drawing.Point(12, 12); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(271, 249); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // cmdCapture // this.cmdCapture.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.cmdCapture.Location = new System.Drawing.Point(109, 276); this.cmdCapture.Name = "cmdCapture"; this.cmdCapture.Size = new System.Drawing.Size(78, 23); this.cmdCapture.TabIndex = 1; this.cmdCapture.Text = "Capture"; this.cmdCapture.UseVisualStyleBackColor = true; this.cmdCapture.Click += new System.EventHandler(this.cmdCapture_Click); // // ScreenCaptureByCopyFromScreen // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(295, 311); this.Controls.Add(this.cmdCapture); this.Controls.Add(this.pictureBox1); this.Text = "Screen Capture"; ((System.ruponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); } private void cmdCapture_Click(object sender, EventArgs e) { Bitmap screen = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height); Graphics g = Graphics.FromImage(screen); g.CopyFromScreen(0, 0, 0, 0, screen.Size); pictureBox1.Image = screen; } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new ScreenCaptureByCopyFromScreen()); } private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Button cmdCapture;
}</source>