using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; using System.Diagnostics; namespace WindowsApplication1 { public partial class Form1 : Form { /// /// 必要なデザイナ変数です。 /// private System.ComponentModel.IContainer components = null; /// /// 使用中のリソースをすべてクリーンアップします。 /// /// マネージ リソースが破棄される場合 true、破棄されない場合は false です。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows フォーム デザイナで生成されたコード /// /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(205, 10); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "キャプチャ"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // comboBox1 // this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBox1.FormattingEnabled = true; this.comboBox1.Items.AddRange(new object[] { "スクリーン全体", "フォーム全体", "コンボボックスのみ", "ボタンのみ"}); this.comboBox1.Location = new System.Drawing.Point(12, 12); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(187, 20); this.comboBox1.TabIndex = 1; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 86); this.Controls.Add(this.comboBox1); this.Controls.Add(this.button1); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.ComboBox comboBox1; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // スクリーン・キャプチャする範囲を決定 Rectangle rc; switch (comboBox1.SelectedItem.ToString()) { case "スクリーン全体": rc = Screen.PrimaryScreen.Bounds; break; case "フォーム全体": rc = this.Bounds; break; case "コンボボックスのみ": rc = this.RectangleToScreen(comboBox1.Bounds); break; case "ボタンのみ": rc = this.RectangleToScreen(button1.Bounds); break; default: rc = Screen.PrimaryScreen.WorkingArea; break; } // Bitmapオブジェクトにスクリーン・キャプチャ Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmp)) { g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy); // ビット・ブロック転送方式の切り替え例: //g.FillRectangle(Brushes.LightPink, 0, 0, rc.Width, rc.Height); //g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceAnd); } // ビットマップ画像として保存して表示 string filePath = @"C:\screen.bmp"; bmp.Save(filePath, ImageFormat.Bmp); Process.Start(filePath); } } }