メイン画面から別画面(サブウィンドウ)をキャプチャしPNGで保存するサンプル

  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using System.Drawing;
  8. namespace WindowsFormsApplication1
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         Form2 form2 = null;
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void button1_Click(object sender, EventArgs e)
  18.         {
  19.             form2 = new Form2();
  20.             //Form2を表示する
  21.             //ここではモーダルダイアログボックスとして表示する
  22.             //オーナーウィンドウにthisを指定する
  23.             form2.Show();
  24.         }
  25.         private void button2_Click(object sender, EventArgs e)
  26.         {
  27.             if (form2 != null)
  28.             {
  29.                 // サンプル画面のサイズでBitmapオブジェクトを作成
  30.                 Bitmap bmp = new Bitmap(form2.Width, form2.Height);
  31.                 Graphics gr = Graphics.FromImage(bmp);
  32.                 // サンプル画面のX座標、Y座標を指定
  33.                 gr.CopyFromScreen(new Point(form2.Location.X, form2.Location.Y), new Point(0, 0), bmp.Size);
  34.                 // 保存
  35.                 bmp.Save("C:\\sample.png", System.Drawing.Imaging.ImageFormat.Png);
  36.                 gr.Dispose();
  37.                 MessageBox.Show("Cドライブ直下に出力しました");
  38.             }
  39.         }
  40.     }
  41. }
image image

実行ファイルダウンロード

戻る