其中各个控件相应的属性设置等等,在Form1.Desigener .cs中有相应的代码,如下:
//
// open
//
this.open.Location = new System.Drawing.Point(37, 46);
this.open.Name = "open";
this.open.Size = new System.Drawing.Size(75, 23);
this.open.TabIndex = 0;
this.open.Text = "打开图像";
this.open.UseVisualStyleBackColor = true;
this.open.Click += new System.EventHandler(this.open_Click);
//
// save
//
this.save.Location = new System.Drawing.Point(37, 92);
this.save.Name = "save";
this.save.Size = new System.Drawing.Size(75, 23);
this.save.TabIndex = 1;
this.save.Text = "保存图像";
this.save.UseVisualStyleBackColor = true;
this.save.Click += new System.EventHandler(this.save_Click);
//
// close
//
this.close.Location = new System.Drawing.Point(37, 138);
this.close.Name = "close";
this.close.Size = new System.Drawing.Size(75, 23);
this.close.TabIndex = 2;
this.close.Text = "关闭";
this.close.UseVisualStyleBackColor = true;
this.close.Click += new System.EventHandler(this.close_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(784, 562);
this.Controls.Add(this.close);
this.Controls.Add(this.save);
this.Controls.Add(this.open);
this.Name = "Form1";
this.Text = "图像处理";
this.Load += new System.EventHandler(this.Form1_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);
this.PerformLayout();
//
在Form1.cs中首先声明如下:
其中需要导入的命名空间有:using System.Drawing;
//文件名
private string curFileName;
//图像对象
private System.Drawing.Bitmap curBitmap;
其余的代码如下:
打开文件——
/// <summary>
/// 打开图像文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void open_Click(object sender, EventArgs e)
{
//创建OpenFileDialog
OpenFileDialog opnDlg = new OpenFileDialog();
//为图像选择一个筛选器
opnDlg.Filter = "所有图像文件|*.bmp;*.pcx;*.png;*.jpg;*.gif;" +
"*.tif;*.ico;*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf|" +
"位图(*.bmp;*.jpg;*.png;...)|*.bmp;*.pcx;*.png;*.jpg;*.gif;*.tif;*.ico|" +
"矢量图(*.wmf;*.eps;*.emf;..)|*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf";
//设置对话框标题
opnDlg.Title = "打开图像文件";
//启用帮助按钮
opnDlg.ShowHelp = true;
//如果选择的结果为打开,则选定文件
if(opnDlg.ShowDialog()==DialogResult.OK)
{
//读取当前选中的文件名称
curFileName = opnDlg.FileName;
}
try
{
//使用 Image.FromFile创建图像对象;
curBitmap = (Bitmap)System.Drawing.Image.FromFile(curFileName);
}
catch(Exception ex)
{
//显示异常;
MessageBox.Show(ex.Message);
}
Invalidate();
}
保存图像——
/// <summary>
/// 保存图像文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void save_Click(object sender, EventArgs e)
{
//如果没有创建图像,则退出
if(curBitmap==null)
{
return;
}
//调用SaveFileDialog
SaveFileDialog saveDlg = new SaveFileDialog();
//设置对话框标题
saveDlg.Title = "保存为";
//读写已存在文件时提示用户
saveDlg.OverwritePrompt = true;
//为图像选择一个筛选器
saveDlg.Filter = "BMP文件(*.bmp)|*.bmp|"+"Gif文件(*.gif)|*.gif|"+
"JPEG文件(*.jpg)|*.jpg|"+"PNG文件(*.png)|*.png";
//启用“帮助”按钮
saveDlg.ShowHelp = true;
//如果选择了格式,则保存图像
if(saveDlg.ShowDialog()==DialogResult.OK)
{
//获取用户选择的文件名
string fileName = saveDlg.FileName;
//获取用户选择的扩展名
string strFilExtn = fileName.Remove(0,fileName.Length-3);
//保存文件
switch(strFilExtn)
{
case "bmp":
//bmp格式;
curBitmap.Save(fileName,System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "jpg":
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "gif":
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
break;
case "tif":
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff);
break;
case "png":
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
break;
default:
break;
}
}
}
关闭窗体——
/// <summary>
/// 关闭窗体,突出程序;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void close_Click(object sender, EventArgs e)
{
this.Close();
}
Paint事件——
很多人不明白这个事件是干什么用的,有了前面的准备工作以后,你要打开一个图像,必须要绘制到Form上,Paint事件就是画图的,在本程序中就是把要打开的图像
画到窗体上;
/// <summary>
/// Paints事件;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Paint(object sender, PaintEventArgs e)
{
//获取Graphics对象;
Graphics g = e.Graphics;
if(curBitmap!=null)
{
//使用DrawImage方法绘制图像
//160,20:即显示在窗体上的坐标
//curBitmap.Width,curBitmap.Height:即要显示的宽度与高度;本例一律原大小显示;
g.DrawImage(curBitmap,160,20,curBitmap.Width,curBitmap.Height);
}
}
————————————————————————————————代码段结束;
运行效果如下图:
(1)打开文件并显示:
(2)保存图像:
posted @
2012-09-03 17:00
木小若
阅读(
1659)
评论()
编辑
收藏
举报