C#利用Picturebox控件显示图片
生活随笔
收集整理的這篇文章主要介紹了
C#利用Picturebox控件显示图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
源文章:https://blog.csdn.net/liyuqian199695/article/details/54098938
C#利用Picturebox控件顯示圖片
?
1、Picturebox控件SizeMode屬性
(1)Normal模式:如果圖片大于Picturebox控件大小,圖片不能完全顯示
(2)AutoSize:自動調整Picturebox控件大小去適應圖片的大小,圖片可以完全顯示。
(3)StretchImage:Picturebox控件大小不變,自動調整圖像適應控件。
2、使用的類
(1)OpenFileDialog 類
提示用戶打開文件。無法繼承此類。
?
public sealed class OpenFileDialog : FileDialog?
OpenFileDialog 類的屬性:
- Filter :獲取或設置當前文件名篩選器字符串,該字符串決定對話框的“另存為文件類型”或“文件類型”框中出現的選擇內容。(從 FileDialog 繼承。)
- FilterIndex :獲取或設置文件對話框中當前選定篩選器的索引。(從 FileDialog 繼承。)
- FileName :獲取或設置一個包含在文件對話框中選定的文件名的字符串。(從 FileDialog 繼承。)
- FileNames:獲取對話框中所有選定文件的文件名。(從 FileDialog 繼承。)
OpenFileDialog 類的公共方法:
?
- ShowDialog?已重載。 運行通用對話框。 (從 CommonDialog 繼承。)
(2)SaveFileDialog 類
?
提供一個對話框,用戶使用該對話框可指定保存文件時使用的選項。
SaveFileDialog 類屬性:
?
- Filter:獲取或設置指定要在 SaveFileDialog 中顯示的文件類型和說明的篩選器字符串。
?
SaveFileDialog 類方法:
?
- ShowDialog 方法:顯示保存對話框控件
?
3、實例
(1)新建一個C#窗體項目,項目名為showPicture,在Form1上添加一個Picturebox控件和兩個按鈕。
(2)添加代碼
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;namespace showPicture {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);} ?}} }(3)顯示效果
(4)保存方法調用效果
總結
以上是生活随笔為你收集整理的C#利用Picturebox控件显示图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 管道命令大全
- 下一篇: 【PPP-RTK技术研究进展与实验验证】