C#winform上传图片
生活随笔
收集整理的這篇文章主要介紹了
C#winform上传图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在窗體上添加上傳圖片按鈕:
// 需要添加的引用 using System.IO; // FileInfo using System.Windows.Forms; // OpenFileDialog// 用到的組件: Button、PictureBox/// <summary> /// 上傳小票Logo /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGetPic_Click(object sender, EventArgs e) {string strPicPath = ""; // 存儲路徑OpenFileDialog openPic = new OpenFileDialog();openPic.Filter = "圖片文件|*.bmp;*.jpg;*.jpeg;*.png";openPic.FilterIndex = 1;//openPic.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//獲取桌面文件夾路徑為初始地址 if (openPic.ShowDialog() == DialogResult.OK){//獲取用戶選擇的文件,并判斷文件大小不能超過20K,fileInfo.Length是以字節為單位的 FileInfo fileInfo = new FileInfo(openPic.FileName);if (fileInfo.Length > 20480){MessageBox.Show("上傳的圖片不能大于20K");}else{strPicPath = openPic.FileName; pictureBox1.BackgroundImage = Image.FromFile(strPicPath); // picReceiptLogo是存儲圖片的路徑}} }總結
以上是生活随笔為你收集整理的C#winform上传图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: solidworks启动慢的原因在这里
- 下一篇: 电脑怎么加快网页打开速度?加快网速。