using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ImageBrowser
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class ImageViewer : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuOpen;
private System.Windows.Forms.MenuItem menuFile;
private System.Windows.Forms.MenuItem menuClose;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.PictureBox pictureBox1;
private string fileName = null;
public ImageViewer()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// デザイナ サポートに必要です。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.menuClose = new System.Windows.Forms.MenuItem();
this.menuOpen = new System.Windows.Forms.MenuItem();
this.menuFile = new System.Windows.Forms.MenuItem();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// menuClose
//
this.menuClose.Index = 1;
this.menuClose.Text = "終了(&X)";
this.menuClose.Click += new System.EventHandler(this.OnClose);
//
// menuOpen
//
this.menuOpen.Index = 0;
this.menuOpen.Text = "開く(&O)";
this.menuOpen.Click += new System.EventHandler(this.OnMenuOpen);
//
// menuFile
//
this.menuFile.Index = 0;
this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuOpen,
this.menuClose});
this.menuFile.Text = "ファイル(&F)";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFile});
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(254, 93);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
//
// ImageViewer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(254, 93);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.pictureBox1});
this.Menu = this.mainMenu1;
this.Name = "ImageViewer";
this.Text = "ImageViewer";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ImageViewer());
}
private void OnMenuOpen(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "JPEG files (*.jpg)|*.jpg|GIF files (*.gif)|*.gif|Bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 1 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog1.FileName;
this.pictureBox1.Invalidate();
}
}
private void DrawImage(Graphics g)
{
try
{
Bitmap myBitmap = new Bitmap( fileName );
g.DrawImage( myBitmap, 0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
}
catch ( System.ArgumentException e)
{
MessageBox.Show(e.Message + "\n画像ファイルを選択してください。", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnClose(object sender, System.EventArgs e)
{
Close();
}
private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if ( fileName != null )
DrawImage(e.Graphics);
}
}
}
|