步驟
s1. 建立窗體應用程式,拖拽兩個button控件和Picturebox控件,如圖所示:

【注】:Picturebox控件SizeMode屬性
(1)Normal:如果圖檔大于Picturebox控件大小,圖檔不能完全顯示
(2)AutoSize:自動調整Picturebox控件大小去适應圖檔的大小,圖檔可以完全顯示。
(3)StretchImage:Picturebox控件大小不變,自動調整圖像适應控件。
s2. 窗體程式如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace sample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string pathname = string.Empty;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.InitialDirectory = ".";
file.Filter = "所有檔案(*.*)|*.*";
file.ShowDialog();
if (file.FileName != string.Empty)
{
try
{
pathname = file.FileName;
this.pictureBox1.Load(pathname);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.ShowDialog();
if (save.FileName != string.Empty)
{
pictureBox1.Image.Save(save.FileName);
}
}
}
}
s3. OpenFileDialog 類-提示使用者打開檔案, 無法繼承此類。
public sealed class OpenFileDialog : FileDialog
OpenFileDialog 類的屬性:
Filter :擷取或設定目前檔案名篩選器字元串,該字元串決定對話框的“另存為檔案類型”或“檔案類型”框中出現的選擇内容。(從 FileDialog 繼承。)
FilterIndex :擷取或設定檔案對話框中目前標明篩選器的索引。(從 FileDialog 繼承。)
FileName :擷取或設定一個包含在檔案對話框中標明的檔案名的字元串。(從 FileDialog 繼承。)
FileNames:擷取對話框中所有標明檔案的檔案名。(從 FileDialog 繼承。)
OpenFileDialog 類的公共方法:
ShowDialog 已重載。 運作通用對話框。 (從 CommonDialog 繼承。)
s4. SaveFileDialog 類:提供一個對話框,使用者使用該對話框可指定儲存檔案時使用的選項。
SaveFileDialog 類屬性:
Filter:擷取或設定指定要在 SaveFileDialog 中顯示的檔案類型和說明的篩選器字元串。
SaveFileDialog 類方法:
ShowDialog 方法:顯示儲存對話框控件
參考文章
1. https://mp.csdn.net/postedit?not_checkout=1