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)保存图像:
查看全文
相关阅读:
Microsoft .NET Custom Errors Not Set(今天公司内审,扫描出了漏洞,要堵住)
马云向***总理专题汇报:坚持为小企业服务
用sp_lock诊断SQL Sever的性能问题(数据库管理员不得不看)转的。
Oracle 10g Audit(审计) 记录登录用户在Oracle中的所有操作(转)
Oracle基本语法集锦(用到了,收集一下)
C#中对注册表的操作指南
生活小知识,生活要认真,驱蚊驱虫驱苍蝇大法
关于TextBox控件onTextChanged时间激活的问题
Monitoring connection pooling behavior
远程共享文件夹的切换访问用户帐号
原文地址:https://www.cnblogs.com/muxiaoruo/p/2669080.html
最新文章
步步为营 SharePoint 开发学习笔记系列 十一、SharePoint 对list操作
C# 导出Excel的示例
步步为营 SharePoint 开发学习笔记系列 八、SharePoint EventHandler开发
步步为营 SharePoint 开发学习笔记系列 十、SharePoint web service 开发(下)
步步为营 SharePoint 开发学习笔记系列 九、SharePoint web service 开发(上)
暑假期间学习不错的网网页
在创建触发器时出现不能在 'inserted' 表和 'deleted' 表中使用 text、ntext 或 image 列
.NET获取服务器信息,如服务器版本、IIS等
SQL Server清空日志及所有表的数据
Oracle激活审计
热门文章
SQL中的存储过程中的事务处理。备忘
让sqlserver恢复到某个时间点(转)
Oracle Grant详解(收集)
oracle中查看用户权限(收藏转载)
BO中的高级权限设置方式。
显示由 IDENT_CURRENT、@@IDENTITY 和 SCOPE_IDENTITY 返回的不同标识值
Excel导入sql server 2005 错误 0xc00470fe 数据流任务 产品级别对于 组件 源 Sheet1$ (1) 而言不足
让Windows 2003也能用移动硬盘
使用OpenFlashChart 2.0结合asp.net的问题(解疑)
oracle数据库sys用户的审计 转载
Copyright © 2011-2022 走看看