C#でスクリーンショットを作成するソフトを作ってみました。
仕様は以下のとうりです。
- フォーム上にスクリーンショットで撮ったイメージを表示します。
- ピクチャーボックスをクリックすると新たにスクリーンショットが撮影されます。
- タスクバーにフォームを表示せずに通知アイコンに表示します。
- 画像のフォーマット形式は現在、JPEG形式、PNG形式、ビットマップ形式、GIF形式、TIFF形式に対応しています。
- アプリケーションを終了させるには、通知アイコンの終了をクリックします。それ以外では終了できません。
ソースコードは以下のとうりです。
using System;
using System.Windows.Forms;
namespace ScreenShot
{
static class Program
{
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread()]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace ScreenShot
{
public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
}
// フォームが呼び出された時にスクリーンショットを表示。
private void Form1_Load(object sender, EventArgs e)
{
this.ScreenShot();
}
// ピクチャーボックスがクリックされた時にもスクリーンショットを表示。
private void pictureBox1_Click(object sender, EventArgs e)
{
this.ScreenShot();
}
// タスクバーアイコンがダブルクリックされた時にもスクリーンショットを表示。
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.ScreenShot();
}
// スクリーンショットを表示させるコンテキストメニュー項目のイベントハンドラ。
private void スクリーンショットSToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.ScreenShot();
}
// アプリケーションを終了させるコンテキストメニュー項目のイベントハンドラ。
private void 終了EToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
Environment.Exit(0);
}
// 画像を保存させるコンテキストメニュー項目のイベントハンドラ。
private void 画像の保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
// ファイルを保存するためのダイアログの設定。
// 初期に開くフォルダーをマイピクチャーにする。
this.saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
// 保存するファイルのフィルター。
// 現在は、JPEG形式、PNG形式、ビットマップ形式、GIF形式、TIFF形式に対応。
this.saveFileDialog1.Filter =
"JEPG形式(*.jpg;*jpeg;*.jpe)|*.jpg;*jpeg;*.jpe|PNG形式(*.png)|*.png|ビットマップ形式(*.bmp;*.dib)|*.bmp;*.dib|GIF形式(*.gif)|*.gif|TIFF形式(*.tif;*.tiff)|*.tif;*.tiff|全てのイメージファイル(*.jpg;*jpeg;*.jpe;*.png;*.bmp;*.dib;*.gif;*.tif;*.tiff)|*.jpg;*jpeg;*.jpe;*.png;*.bmp;*.dib;*.gif;*.tif;*.tiff|全てのファイル(*.*)|*.*";
// ダイアログのウィンドウタイトルの設定。
this.saveFileDialog1.Title = "保存するファイルを選択してください。";
// ダイアログを表示。
if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
int fi = this.saveFileDialog1.FilterIndex;
Bitmap bmp = ((Bitmap)(this.pictureBox1.Image));
// JPEG形式
if (fi == 1)
{
bmp.Save(this.saveFileDialog1.OpenFile(), ImageFormat.Jpeg);
}
// PNG形式
else if (fi == 2)
{
bmp.Save(this.saveFileDialog1.OpenFile(), ImageFormat.Png);
}
// ビットマップ形式
else if (fi == 3)
{
bmp.Save(this.saveFileDialog1.OpenFile(), ImageFormat.Bmp);
}
// GIF形式
else if (fi == 4)
{
bmp.Save(this.saveFileDialog1.OpenFile(), ImageFormat.Gif);
}
// TIFF形式
else if (fi == 5)
{
bmp.Save(this.saveFileDialog1.OpenFile(), ImageFormat.Tiff);
}
// イメージ形式を拡張子で指定
else
{
string fe = Path.GetExtension(this.saveFileDialog1.FileName);
// JPEG形式
if (fe == ".jpg" || fe == ".jpeg" || fe == ".jpe")
{
bmp.Save(this.saveFileDialog1.OpenFile(), ImageFormat.Jpeg);
}
// PNG形式
else if (fe == ".png")
{
bmp.Save(this.saveFileDialog1.OpenFile(), ImageFormat.Png);
}
// ビットマップ形式
else if (fe == ".bmp" || fe == ".dib")
{
bmp.Save(this.saveFileDialog1.OpenFile(), ImageFormat.Bmp);
}
// GIF形式
else if (fe == "gif")
{
bmp.Save(this.saveFileDialog1.OpenFile(), ImageFormat.Gif);
}
// TIFF形式
else if (fe == "tif" || fe == "tiff")
{
bmp.Save(this.saveFileDialog1.OpenFile(), ImageFormat.Tiff);
}
// その他形式
else
{
bmp.Save(this.saveFileDialog1.FileName);
}
}
}
}
// フォームを最大化させるためのコンテキストメニュー項目のイベントハンドラ。
private void 最大化BToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
// フォームを最小化させるためのコンテキストメニュー項目のイベントハンドラ。
private void 最小化TToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
// フォームを元の大きさに戻すためのコンテキストメニュー項目のイベントハンドラ。
private void 普通表示WToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
// フォームが閉じられてもアプリケーションを終了させないようにする。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
// スクリーンショットを行うためのコード。
private void ScreenShot()
{
// ディスプレイの高さを取得する。
int h = Screen.PrimaryScreen.Bounds.Height;
// ディスプレイの幅を取得する。
int w = Screen.PrimaryScreen.Bounds.Width;
// ウィンドウサイズをディスプレイサイズの1/2にする。
this.ClientSize = new Size(w / 2, h / 2);
// スクリーンショットの保存する変数。
Bitmap bmp = new Bitmap(w, h);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), bmp.Size);
}
this.pictureBox1.Image = bmp;
}
}
}
namespace ScreenShot
{
partial class Form1
{
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
/// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows フォーム デザイナーで生成されたコード
/// <summary>
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
/// コード エディターで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.スクリーンショットSToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripSeparator();
this.画像の保存ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripSeparator();
this.普通表示WToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.最大化BToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.最小化TToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.終了EToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.ContextMenuStrip = this.contextMenuStrip1;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(284, 262);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.スクリーンショットSToolStripMenuItem1,
this.toolStripMenuItem1,
this.画像の保存ToolStripMenuItem,
this.toolStripMenuItem2,
this.普通表示WToolStripMenuItem,
this.最大化BToolStripMenuItem,
this.最小化TToolStripMenuItem,
this.終了EToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(261, 148);
//
// スクリーンショットSToolStripMenuItem1
//
this.スクリーンショットSToolStripMenuItem1.Font = new System.Drawing.Font("Meiryo UI", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.スクリーンショットSToolStripMenuItem1.Name = "スクリーンショットSToolStripMenuItem1";
this.スクリーンショットSToolStripMenuItem1.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Alt)
| System.Windows.Forms.Keys.F1)));
this.スクリーンショットSToolStripMenuItem1.Size = new System.Drawing.Size(260, 22);
this.スクリーンショットSToolStripMenuItem1.Text = "スクリーンショット(&S)";
this.スクリーンショットSToolStripMenuItem1.Click += new System.EventHandler(this.スクリーンショットSToolStripMenuItem1_Click);
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(257, 6);
//
// 画像の保存ToolStripMenuItem
//
this.画像の保存ToolStripMenuItem.Name = "画像の保存ToolStripMenuItem";
this.画像の保存ToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Alt)
| System.Windows.Forms.Keys.F2)));
this.画像の保存ToolStripMenuItem.Size = new System.Drawing.Size(260, 22);
this.画像の保存ToolStripMenuItem.Text = "画像の保存";
this.画像の保存ToolStripMenuItem.Click += new System.EventHandler(this.画像の保存ToolStripMenuItem_Click);
//
// toolStripMenuItem2
//
this.toolStripMenuItem2.Name = "toolStripMenuItem2";
this.toolStripMenuItem2.Size = new System.Drawing.Size(257, 6);
//
// 普通表示WToolStripMenuItem
//
this.普通表示WToolStripMenuItem.Name = "普通表示WToolStripMenuItem";
this.普通表示WToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Alt)
| System.Windows.Forms.Keys.F3)));
this.普通表示WToolStripMenuItem.Size = new System.Drawing.Size(260, 22);
this.普通表示WToolStripMenuItem.Text = "普通表示(&W)";
this.普通表示WToolStripMenuItem.Click += new System.EventHandler(this.普通表示WToolStripMenuItem_Click);
//
// 最大化BToolStripMenuItem
//
this.最大化BToolStripMenuItem.Name = "最大化BToolStripMenuItem";
this.最大化BToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Alt)
| System.Windows.Forms.Keys.F4)));
this.最大化BToolStripMenuItem.Size = new System.Drawing.Size(260, 22);
this.最大化BToolStripMenuItem.Text = "最大化(&B)";
this.最大化BToolStripMenuItem.Click += new System.EventHandler(this.最大化BToolStripMenuItem_Click);
//
// 最小化TToolStripMenuItem
//
this.最小化TToolStripMenuItem.Name = "最小化TToolStripMenuItem";
this.最小化TToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Alt)
| System.Windows.Forms.Keys.F5)));
this.最小化TToolStripMenuItem.Size = new System.Drawing.Size(260, 22);
this.最小化TToolStripMenuItem.Text = "最小化(&T)";
this.最小化TToolStripMenuItem.Click += new System.EventHandler(this.最小化TToolStripMenuItem_Click);
//
// 終了EToolStripMenuItem
//
this.終了EToolStripMenuItem.Name = "終了EToolStripMenuItem";
this.終了EToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Alt)
| System.Windows.Forms.Keys.F6)));
this.終了EToolStripMenuItem.Size = new System.Drawing.Size(260, 22);
this.終了EToolStripMenuItem.Text = "終了(&E)";
this.終了EToolStripMenuItem.Click += new System.EventHandler(this.終了EToolStripMenuItem_Click);
//
// notifyIcon1
//
this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "notifyIcon1";
this.notifyIcon1.Visible = true;
this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.ContextMenuStrip = this.contextMenuStrip1;
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Text = "Form1";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem スクリーンショットSToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem 終了EToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem 画像の保存ToolStripMenuItem;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem2;
private System.Windows.Forms.ToolStripMenuItem 最小化TToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 最大化BToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 普通表示WToolStripMenuItem;
}
}
スクリーンショットで撮影した写真です。
感想
スクリーンショットを撮るにはPrint Screenキーを押せば簡単に取れますが、
ペイントを起動してペーストするのが大変です。
こういうソフトを作ってみるのもいいなと思いました。
色々応用したいです。
「C#でスクリーンショット作成」への1件のフィードバック