Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / C#

How to capture a Window as an Image and save it

Rate me:
Please Sign up or sign in to vote.
4.43/5 (12 votes)
15 Jun 2007CPOL3 min read 142.9K   6.4K   91  
Take a snapshot of the main Window of any UI application
/*
 * Please leave this Copyright notice in your code if you use it
 * Written by Decebal Mihailescu [http://www.codeproject.com/script/articles/list_articles.asp?userid=634640]
 */
namespace SnapShot
{
    partial class LocalSnapShotForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LocalSnapShotForm));
            this.btnSnapSot = new System.Windows.Forms.Button();
            this._pictureBox = new System.Windows.Forms.PictureBox();
            this.btnSaveImage = new System.Windows.Forms.Button();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.label1 = new System.Windows.Forms.Label();
            this.textBoxClass = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.textBoxCaption = new System.Windows.Forms.TextBox();
            this.btn_FindWnd = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.lblHwnd = new System.Windows.Forms.Label();
            this.ckbClientWnd = new System.Windows.Forms.CheckBox();
            this.cmbProcs = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            ((System.ComponentModel.ISupportInitialize)(this._pictureBox)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // btnSnapSot
            // 
            this.btnSnapSot.Enabled = false;
            this.btnSnapSot.Location = new System.Drawing.Point(278, 9);
            this.btnSnapSot.Name = "btnSnapSot";
            this.btnSnapSot.Size = new System.Drawing.Size(75, 23);
            this.btnSnapSot.TabIndex = 0;
            this.btnSnapSot.Text = "SnapSot";
            this.toolTip1.SetToolTip(this.btnSnapSot, "Capture Selected Window");
            this.btnSnapSot.UseVisualStyleBackColor = true;
            this.btnSnapSot.Click += new System.EventHandler(this.btnSnapShot_Click);
            // 
            // _pictureBox
            // 
            this._pictureBox.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._pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this._pictureBox.Location = new System.Drawing.Point(0, 143);
            this._pictureBox.Name = "_pictureBox";
            this._pictureBox.Size = new System.Drawing.Size(385, 259);
            this._pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this._pictureBox.TabIndex = 1;
            this._pictureBox.TabStop = false;
            this.toolTip1.SetToolTip(this._pictureBox, "Latest imaged captured");
            // 
            // btnSaveImage
            // 
            this.btnSaveImage.Enabled = false;
            this.btnSaveImage.Location = new System.Drawing.Point(278, 40);
            this.btnSaveImage.Name = "btnSaveImage";
            this.btnSaveImage.Size = new System.Drawing.Size(75, 23);
            this.btnSaveImage.TabIndex = 2;
            this.btnSaveImage.Text = "SaveImage...";
            this.toolTip1.SetToolTip(this.btnSaveImage, "Save current image");
            this.btnSaveImage.UseVisualStyleBackColor = true;
            this.btnSaveImage.Click += new System.EventHandler(this.btnSaveImage_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(5, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(34, 13);
            this.label1.TabIndex = 3;
            this.label1.Text = "class:";
            // 
            // textBoxClass
            // 
            this.textBoxClass.Location = new System.Drawing.Point(59, 9);
            this.textBoxClass.Name = "textBoxClass";
            this.textBoxClass.Size = new System.Drawing.Size(183, 20);
            this.textBoxClass.TabIndex = 4;
            this.toolTip1.SetToolTip(this.textBoxClass, "window class");
            this.textBoxClass.TextChanged += new System.EventHandler(this.textBox_TextChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(5, 47);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(45, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "caption:";
            // 
            // textBoxCaption
            // 
            this.textBoxCaption.Location = new System.Drawing.Point(59, 40);
            this.textBoxCaption.Name = "textBoxCaption";
            this.textBoxCaption.Size = new System.Drawing.Size(183, 20);
            this.textBoxCaption.TabIndex = 4;
            this.toolTip1.SetToolTip(this.textBoxCaption, "window class");
            this.textBoxCaption.TextChanged += new System.EventHandler(this.textBox_TextChanged);
            // 
            // btn_FindWnd
            // 
            this.btn_FindWnd.Enabled = false;
            this.btn_FindWnd.Location = new System.Drawing.Point(144, 75);
            this.btn_FindWnd.Name = "btn_FindWnd";
            this.btn_FindWnd.Size = new System.Drawing.Size(97, 24);
            this.btn_FindWnd.TabIndex = 5;
            this.btn_FindWnd.Text = "Find Window";
            this.toolTip1.SetToolTip(this.btn_FindWnd, "Find a window based on Window class & Title");
            this.btn_FindWnd.UseVisualStyleBackColor = true;
            this.btn_FindWnd.Click += new System.EventHandler(this.btnFindWnd_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.lblHwnd);
            this.groupBox1.Controls.Add(this.btn_FindWnd);
            this.groupBox1.Controls.Add(this.textBoxCaption);
            this.groupBox1.Controls.Add(this.textBoxClass);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Location = new System.Drawing.Point(12, 3);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(260, 105);
            this.groupBox1.TabIndex = 6;
            this.groupBox1.TabStop = false;
            // 
            // lblHwnd
            // 
            this.lblHwnd.AutoSize = true;
            this.lblHwnd.Location = new System.Drawing.Point(11, 75);
            this.lblHwnd.Name = "lblHwnd";
            this.lblHwnd.Size = new System.Drawing.Size(38, 13);
            this.lblHwnd.TabIndex = 6;
            this.lblHwnd.Text = "Hwnd:";
            this.toolTip1.SetToolTip(this.lblHwnd, "The current window handle in hex");
            // 
            // ckbClientWnd
            // 
            this.ckbClientWnd.AutoSize = true;
            this.ckbClientWnd.Location = new System.Drawing.Point(278, 78);
            this.ckbClientWnd.Name = "ckbClientWnd";
            this.ckbClientWnd.Size = new System.Drawing.Size(94, 17);
            this.ckbClientWnd.TabIndex = 7;
            this.ckbClientWnd.Text = "Client Window";
            this.toolTip1.SetToolTip(this.ckbClientWnd, "Get only the client area, not the mainframe");
            this.ckbClientWnd.UseVisualStyleBackColor = true;
            // 
            // cmbProcs
            // 
            this.cmbProcs.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbProcs.FormattingEnabled = true;
            this.cmbProcs.Location = new System.Drawing.Point(71, 116);
            this.cmbProcs.Name = "cmbProcs";
            this.cmbProcs.Size = new System.Drawing.Size(282, 21);
            this.cmbProcs.TabIndex = 8;
            this.toolTip1.SetToolTip(this.cmbProcs, "The UI Process & Title");
            this.cmbProcs.SelectedIndexChanged += new System.EventHandler(this.cmbProcs_SelectedIndexChanged);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(12, 120);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(43, 13);
            this.label3.TabIndex = 9;
            this.label3.Text = "UI App:";
            // 
            // LocalSnapShotForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(385, 402);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.cmbProcs);
            this.Controls.Add(this.ckbClientWnd);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.btnSaveImage);
            this.Controls.Add(this._pictureBox);
            this.Controls.Add(this.btnSnapSot);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "LocalSnapShotForm";
            this.Text = "SnapShot";
            this.Shown += new System.EventHandler(this.LocalSnapShotForm_Shown);
            this.Load += new System.EventHandler(this.LocalSnapShotForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this._pictureBox)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnSnapSot;
        private System.Windows.Forms.PictureBox _pictureBox;
        private System.Windows.Forms.Button btnSaveImage;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBoxClass;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBoxCaption;
        private System.Windows.Forms.Button btn_FindWnd;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.CheckBox ckbClientWnd;
        private System.Windows.Forms.ComboBox cmbProcs;
        private System.Windows.Forms.Label lblHwnd;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.ToolTip toolTip1;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.

Comments and Discussions