本文實(shí)例講述了C#中OpenFileDialog和PictrueBox的用法。。具體用法分析如下:
先來(lái)看看這段代碼:
代碼如下:string resultFile = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D://Patch";
openFileDialog1.Filter = "All files (*.*)|*.*|txt files (*.txt)|*.txt";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
resultFile = openFileDialog1.FileName;
resultFile 就能得到你選中文件的路徑
OpenFileDialog控件有以下基本屬性
InitialDirectory 對(duì)話框的初始目錄
Filter 要在對(duì)話框中顯示的文件篩選器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*"
FilterIndex 在對(duì)話框中選擇的文件篩選器的索引,如果選第一項(xiàng)就設(shè)為1
RestoreDirectory 控制對(duì)話框在關(guān)閉之前是否恢復(fù)當(dāng)前目錄
FileName 第一個(gè)在對(duì)話框中顯示的文件或最后一個(gè)選取的文件
Title 將顯示在對(duì)話框標(biāo)題欄中的字符
AddExtension 是否自動(dòng)添加默認(rèn)擴(kuò)展名
CheckPathExists 在對(duì)話框返回之前,檢查指定路徑是否存在
DefaultExt 默認(rèn)擴(kuò)展名
DereferenceLinks 在從對(duì)話框返回前是否取消引用快捷方式
ShowHelp 啟用"幫助"按鈕
ValiDateNames 控制對(duì)話框檢查文件名中是否不含有無(wú)效的字符或序列
怎樣設(shè)置OpenFileDialog組件的Filter,使實(shí)現(xiàn)一次過濾出多種擴(kuò)展名的文件?
代碼如下:dlg.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.* "
第一個(gè)參數(shù)是picturebox的寬度,第二個(gè)是picturebox的高度,第三個(gè)是你的圖片。這個(gè)方法可以把圖片調(diào)整到合適的大小。你就不要設(shè)置SizeMode的屬性了,通過這個(gè)方法得到合適的圖片后,設(shè)置picturebox的image屬性等于這個(gè)圖片,不要設(shè)置背景圖。我沒有測(cè)試。你自己去測(cè)試下吧,如果還是有問題,那就是圖片太小了。你要重新做張圖
代碼如下:public Image GetNewImage(int newImgWidth, int newImgHeight, Image srcImage)
{
Image newImg = srcImage.GetThumbnailImage(newImgWidth, newImgHeight, null, new IntPtr());
Graphics gr = Graphics.FromImage(newImg);
gr.DrawImage(newImg, 0, 0, newImg.Width, newImg.Height);
gr.Dispose();
return newImg;
}
PictrueBox的SizeMode屬性:
代碼如下:// 摘要:
// 圖像被置于 System.Windows.Forms.PictureBox 的左上角。如果圖像比包含它的 System.Windows.Forms.PictureBox
// 大,則該圖像將被剪裁掉。
Normal = 0,
新聞熱點(diǎn)
疑難解答