該系列讀書(shū)筆記為《C#數(shù)字圖像處理算法典型事例》(趙春江 編著,人民郵電出版社,2009)讀書(shū)筆記。 詳細(xì)內(nèi)容,請(qǐng)參考原始圖書(shū)。 ================================================
代碼如下: 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 = "打開(kāi)圖形文件"; 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; } //設(shè)定文件格式,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); } } } } 結(jié)果如圖: 需要注意的問(wèn)題: 在寫(xiě)好paint事件后,需要在窗體的事件中進(jìn)行關(guān)聯(lián),開(kāi)始沒(méi)有關(guān)聯(lián),所以總是不能顯示。 如下: |
|
來(lái)自: Cloud書(shū)屋 > 《圖像處理》