react 前端框架如何驱动企业数字化转型与创新发展
1005
2022-12-02
读入图像文件并显示【C#图像处理学习笔记】
说明:
该系列读书笔记为《C#数字图像处理算法典型事例》(赵春江 编著,人民邮电出版社,2009)读书笔记。
详细内容,请参考原始图书。
================================================
代码如下:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Demo1{public partial class Form1 : Form{private string curFileName;private System.Drawing.Bitmap curBitmap;public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){}private void open_Click(object sender, EventArgs e){OpenFileDialog opndlg = new OpenFileDialog();opndlg.Filter = "所有文件|*.bmp;*.pcx;*.png;*.jpg;*.gif;" +"*.tif;*.ico;*.dcx;*.cgm;*.cdr;*.wmf;*.eps;*.emf;|" +"位图(*.bmp;*.jpg;*.png;...)|*.bmp;*.pcx;*.png;*.jpg;*.gif;*.tif;*.ico|" +"矢量图(*.wmf;*.eps;*.emf;...)|*.dcf;*.cgm;*.cdr;*.wmf;*.eps;*.emf";opndlg.Title = "打开图形文件";opndlg.ShowHelp = true;if (opndlg.ShowDialog() == DialogResult.OK){curFileName = opndlg.FileName;try{curBitmap = (Bitmap)Image.FromFile(curFileName);}catch (Exception exp){MessageBox.Show(exp.Message);}}Invalidate();}private void save_Click(object sender, EventArgs e){if (curBitmap == null)return;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":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;}//设定文件格式,Ctrl+E,D}}private void close_Click(object sender, EventArgs e){this.Close();}private void Form1_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;if (curBitmap != null){g.DrawImage(curBitmap, 160, 20, curBitmap.Width, curBitmap.Height);}}}}
结果如图:
需要注意的问题:
在写好paint事件后,需要在窗体的事件中进行关联,开始没有关联,所以总是不能显示。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~