zoukankan
html css js c++ java
[转]C#Windows窗体打开图像与保存
转载自:
http://blog.csdn.net/gufengcangying
其中各个控件相应的属性设置等等,在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)保存图像:
查看全文
相关阅读:
[经验交流] kubernetes v1.11 更新了高可用方案
Java泛型和编译优化的一个例子
Java泛型和编译优化的一个例子
Java泛型和编译优化的一个例子
浅析JSONP-解决Ajax跨域访问问题
java设计原则---开闭原则
for,foreach,iterator的用法和区别
List,Set,Map三种接口的区别
为什么接口中定义的变量必须为常量?
重写,string创建内存问题
原文地址:https://www.cnblogs.com/muxiaoruo/p/2669080.html
最新文章
xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
DBA知道这17条Linux命令,就够了!
SpringCloud Alibaba微服务实战十五
做报表5年,被批Excel毫无新意,能开发的可视化报表让我重获新生
客户端连接关闭,服务端报:Broken pipe
Broken pipe 客户端关闭写后,继续发数据:
客户端报:Connection reset by peer
热门文章
服务端close_wait 服务端有问题,客户端close_wait 客户端有问题
客户端出现close_wait
调用 close 函数,设置了 SO_LINGER 为 true
一方突然断电重启,之前建立的连接信息丢失,另一方并不知道
centos7 firewall-cmd 理解多区域配置中的 firewalld 防火墙
centos7 firewall-cmd 理解多区域配置中的 firewalld 防火墙
centos7 firewall-cmd 理解多区域配置中的 firewalld 防火墙
[经验交流] kubernetes v1.11 更新了高可用方案
[经验交流] kubernetes v1.11 更新了高可用方案
[经验交流] kubernetes v1.11 更新了高可用方案
Copyright © 2011-2022 走看看