使用者可自行選擇,把大量的影像做合併,顯示於PictureBox與儲存成影像檔
主要會撰寫這個程式的原因,在於看到藍色小舖上有一個問題一直都沒有人去解,問題是批次圖檔合併,對於此問題我還蠻有興趣的,隨即開始找了一些解決的方法,我的概念大概分成兩個部份
第一部分:將資料夾內要合併的圖檔名稱顯示於Listbox
在此部份,上網找了關於如何快速獲取檔案名稱的範例,很慶幸在章立民老師的網站看到此相關文章,有興趣的同學的可以去看看
Visual C# 2005 - 如何取得某目錄之下所有檔案名稱的字串集合
http://blog.xuite.net/alwaysfuturevision/liminzhang/8741076
第二部份:讀取與合併影像
在此部份,是透過 Bitmap 與 PictureBox 來完成
主要的流程如下
1. 把要合併影像,宣告為 Bitmap 陣列
2. 開一張新的畫布來存放要合併的影像,在本程式中,我是將影像橫向的串接合併,因此寬度為所有影像的寬度總和,
高度的話我是找出最高的影像的高度值當作新畫布的高度。
3. 依序將要合併的影像加到新的 Bitmap 物件裡
4. 將合併結果顯示於PictureBox以及儲存成影像檔
以下為程式碼
01
using System;
02
using System.Collections.Generic;
03
using System.ComponentModel;
04
using System.Data;
05
using System.Drawing;
06
using System.Text;
07
using System.Windows.Forms;
08
using Microsoft.VisualBasic;
09
using Microsoft.VisualBasic.ApplicationServices;
10
using Microsoft.VisualBasic.CompilerServices;
11
using Microsoft.VisualBasic.Devices;
12
using Microsoft.VisualBasic.FileIO;
13
using Microsoft.VisualBasic.Logging;
14
using Microsoft.VisualBasic.MyServices;
15
16
17
namespace WindowsApplication2
18
{
19
public partial class Form1 : Form
20
{
21
public Form1()
22
{
23
InitializeComponent();
24
}
25
26
private void button1_Click(object sender, EventArgs e)
27
{
28
// 開啟pic1目錄內的檔案名稱,並且加入listBox1
29
Computer MyComputer = new Computer();
30
foreach(string FlagFileName in MyComputer.FileSystem.GetFiles(MyComputer.FileSystem.CurrentDirectory + @"/pic1"))
31
{
32
33
listBox1.Items.Add(FlagFileName);
34
}
35
}
36
37
private void button2_Click(object sender, EventArgs e)
38
{
39
// 開啟pic2目錄內的檔案名稱,並且加入listBox2
40
Computer MyComputer = new Computer();
41
foreach (string FlagFileName in MyComputer.FileSystem.GetFiles(MyComputer.FileSystem.CurrentDirectory + @"/pic2"))
42
{
43
44
listBox2.Items.Add(FlagFileName);
45
}
46
}
47
48
private void button3_Click(object sender, EventArgs e)
49
{
50
// 將要合併的圖檔檔名加入listbox3
51
listBox3.Items.Add(listBox2.SelectedItem.ToString());
52
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
53
}
54
55
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
56
{
57
// 當選擇listbox1內的檔名時,將圖show到pictureBox2
58
if (listBox2.SelectedIndex > -1)
59
{
60
this.pictureBox2.ImageLocation = listBox2.SelectedItem.ToString();
61
}
62
}
63
64
private void button4_Click(object sender, EventArgs e)
65
{
66
// 合併圖檔
67
Bitmap[] Capture = new Bitmap[listBox3.Items.Count]; // 要合併的多張影像
68
int ImageW = 0;
69
int ImageH = 0;
70
71
for (int i = 0; i < listBox3.Items.Count; i++)
72
{
73
Capture[i] = new Bitmap(listBox3.Items[i].ToString());
74
ImageW += Capture[i].Width;
75
if (Capture[i].Height > ImageH)
76
{
77
ImageH = Capture[i].Height;
78
}
79
80
}
81
82
Bitmap x = new Bitmap(ImageW, ImageH + 400);
83
Graphics g = Graphics.FromImage(x);
84
85
int tempW = 0;
86
for (int i = 0; i < listBox3.Items.Count; i++)
87
{
88
g.DrawImage(Capture[i], tempW, 0, Capture[i].Width, Capture[i].Height);
89
tempW = tempW + Capture[i].Width ;
90
91
}
92
93
pictureBox3.Image = x;
94
pictureBox3.Image.Save(@"./result.bmp");
95
96
}
97
98
}
99
}
using System; 02
using System.Collections.Generic; 03
using System.ComponentModel; 04
using System.Data; 05
using System.Drawing; 06
using System.Text; 07
using System.Windows.Forms; 08
using Microsoft.VisualBasic; 09
using Microsoft.VisualBasic.ApplicationServices; 10
using Microsoft.VisualBasic.CompilerServices; 11
using Microsoft.VisualBasic.Devices; 12
using Microsoft.VisualBasic.FileIO; 13
using Microsoft.VisualBasic.Logging; 14
using Microsoft.VisualBasic.MyServices; 15
16
17
namespace WindowsApplication2 18
{ 19
public partial class Form1 : Form 20
{ 21
public Form1() 22
{ 23
InitializeComponent(); 24
} 25
26
private void button1_Click(object sender, EventArgs e) 27
{ 28
// 開啟pic1目錄內的檔案名稱,並且加入listBox1 29
Computer MyComputer = new Computer(); 30
foreach(string FlagFileName in MyComputer.FileSystem.GetFiles(MyComputer.FileSystem.CurrentDirectory + @"/pic1")) 31
{ 32
33
listBox1.Items.Add(FlagFileName); 34
} 35
} 36
37
private void button2_Click(object sender, EventArgs e) 38
{ 39
// 開啟pic2目錄內的檔案名稱,並且加入listBox2 40
Computer MyComputer = new Computer(); 41
foreach (string FlagFileName in MyComputer.FileSystem.GetFiles(MyComputer.FileSystem.CurrentDirectory + @"/pic2")) 42
{ 43
44
listBox2.Items.Add(FlagFileName); 45
} 46
} 47
48
private void button3_Click(object sender, EventArgs e) 49
{ 50
// 將要合併的圖檔檔名加入listbox3 51
listBox3.Items.Add(listBox2.SelectedItem.ToString()); 52
listBox2.Items.RemoveAt(listBox2.SelectedIndex); 53
} 54
55
private void listBox2_SelectedIndexChanged(object sender, EventArgs e) 56
{ 57
// 當選擇listbox1內的檔名時,將圖show到pictureBox2 58
if (listBox2.SelectedIndex > -1) 59
{ 60
this.pictureBox2.ImageLocation = listBox2.SelectedItem.ToString(); 61
} 62
} 63
64
private void button4_Click(object sender, EventArgs e) 65
{ 66
// 合併圖檔 67
Bitmap[] Capture = new Bitmap[listBox3.Items.Count]; // 要合併的多張影像 68
int ImageW = 0; 69
int ImageH = 0; 70
71
for (int i = 0; i < listBox3.Items.Count; i++) 72
{ 73
Capture[i] = new Bitmap(listBox3.Items[i].ToString()); 74
ImageW += Capture[i].Width; 75
if (Capture[i].Height > ImageH) 76
{ 77
ImageH = Capture[i].Height; 78
} 79
80
} 81
82
Bitmap x = new Bitmap(ImageW, ImageH + 400); 83
Graphics g = Graphics.FromImage(x); 84
85
int tempW = 0; 86
for (int i = 0; i < listBox3.Items.Count; i++) 87
{ 88
g.DrawImage(Capture[i], tempW, 0, Capture[i].Width, Capture[i].Height); 89
tempW = tempW + Capture[i].Width ; 90
91
} 92
93
pictureBox3.Image = x; 94
pictureBox3.Image.Save(@"./result.bmp"); 95
96
} 97
98
} 99
}
執行結果
using