- 載入 AForge.NET 參考
- 實作 openFileDialog 讀圖
- 實作 saveFileDialog 存圖
- 開啟檔案「手動」瀏灠器載入圖。
- 以絕對路徑「自動」載入圖。
- 將圖放至專案/bin/Debug 目錄內「自動」載入圖。
Step1.
檔案 → 新增專案 → Windows Form 應用程式 → 拉物件 Form1.cs[設計] → 撰寫 Form1.cs
Step2.
方案總管 → 參考 → 右鍵加入參考 → 瀏灠(預設路徑) C:\Program Files (x86)\AForge.NET\Framework\Release → 加入參考
- AForge.Imaging.dll
- AForge.Imaging.Formats.dll
Step3.
拉入 Windows Form:
- pictureBox × 1
- button × 4
- openFileDialog × 1
- saveFileDialog × 1
Step4. Coding
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using AForge.Imaging; using AForge.Imaging.Formats; namespace Lesson_8 { public partial class Form1 : Form { Bitmap imgone; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 手動讀圖 → 開啟檔案瀏灠器 openFileDialog1.Filter = "所有檔案(*.*)|*.*"; if(openFileDialog1.ShowDialog()==DialogResult.OK){ imgone =ImageDecoder.DecodeFromFile(openFileDialog1.FileName); pictureBox1.Image = imgone; } } private void button2_Click(object sender, EventArgs e) { saveFileDialog1.FileName = @"imgone.bmp"; saveFileDialog1.Filter = "Bitmap file(*.bmp)|*.bmp"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { imgone.Save(saveFileDialog1.FileName); } } private void button3_Click(object sender, EventArgs e) { // 自動讀圖 → 絕對路徑 imgone = ImageDecoder.DecodeFromFile(@"D:\Visual C Sharp\Img\lana.png"); pictureBox1.Image = imgone; } private void button4_Click(object sender, EventArgs e) { // 自動讀圖2 → 圖檔要放至「專案/bin/Debug」目錄 imgone = ImageDecoder.DecodeFromFile("lana.png"); pictureBox1.Image = imgone; } private void Form1_Load(object sender, EventArgs e) { } } }
沒有留言:
張貼留言