c# 用PictureBox打開圖片

剛剛學習了同事編寫的在窗體中打開圖片的代碼,自己也試著做了一例子,以利於學習。
例子如下:
 在下列圖片框中雙擊PictureBox插入圖片,並下圖片下方顯示圖片信息。如下圖結果。
c# 用PictureBox打開圖片

代碼如下:
using  System.IO;


namespace  newTest
{
    
public  partial  class  Form1 : Form
    {
        
public  Form1()
        {
            InitializeComponent();
            pcbPic.DoubleClick 
+=   new  EventHandler(pcbPic_DoubleClick);
        }

        
void  pcbPic_DoubleClick( object  sender, EventArgs e)
        {
            OpenFileDialog ofdPic 
=   new  OpenFileDialog();
            
// 取得或設定目前的檔名擴展名,以決定出現在對話方塊中[檔案類型] 的選項。
            ofdPic.Filter  =   " JPG(*.JPG;*.JPEG);gif文件(*.GIF)|*.jpg;*.jpeg;*.gif " ;
            
// 取得或設定檔案對話方塊中目前所選取之篩選條件的索引
            ofdPic.FilterIndex  =   1 ;
            
// 關閉對話框,還原當前的目錄
            ofdPic.RestoreDirectory  =   true ;
            
// 取得或設定含有檔案對話方塊中所選文件的名稱。
            ofdPic.FileName  =   "" ;
            
if  (ofdPic.ShowDialog()  ==  DialogResult.OK)
            {
                
// 得到文件名及路徑
                 string  sPicPaht  =  ofdPic.FileName.ToString();

                
// FileInfo:提供建立、複製、刪除、移動和開啟檔案的執行個體 (Instance) 方法
                FileInfo fiPicInfo  =   new  FileInfo(sPicPaht);
                
// Length:取得目前檔案的大小。以字節為單位
                 long  lPicLong  =  fiPicInfo.Length  /   1024 ;
                
// 得到文名
                 string  sPicName  =  fiPicInfo.Name;
                
// 取得父目錄
                 string  sPicDirectory  =  fiPicInfo.Directory.ToString();
                
// DirectoryName :取得表示目錄完整路徑。
                 string  sPicDirectoryPath  =  fiPicInfo.DirectoryName;



                
// 封裝GDI+點陣圖像,是用來處理像素資料所定義影像的物件。
                
// Bitmap類:封裝GDI+ 點陣圖,這個點陣圖是由圖形影像的像素資料及其屬性所組成。Bitmap 是用來處理像素資料所定義影像的物件。
                Bitmap bmPic  =   new  Bitmap(sPicPaht);

                
// 如果文件大於500KB,警告
                 if  (lPicLong  >   400 )
                {
                    MessageBox.Show(
" 此文件大小為 "   +  lPicLong  +   " K;已超過最大限制的400K范圍! " );
                }
                
else
                {
                   
                    Point ptLoction 
=   new  Point(bmPic.Size);
                    
if  (ptLoction.X  >  pcbPic.Size.Width  ||  ptLoction.Y  >  pcbPic.Size.Height)
                    {
                        
// 圖像框的停靠方式
                        
// pcbPic.Dock = DockStyle.Fill;

                        
// 圖像充滿圖像框,並且圖像維持比例
                        pcbPic.SizeMode  =  PictureBoxSizeMode.Zoom; 
                    }
                    
else
                    {
                        
// 圖像在圖像框置中
                        pcbPic.SizeMode  =  PictureBoxSizeMode.CenterImage;
                    }
                }
                
// LoadAsync:非同步載入圖像
                pcbPic.LoadAsync(sPicPaht);

                
// 顯示圖像名 lblNameValue:label控件
                lblNameValue.Text  =  sPicName;
                
// 顯示圖像大小  lblLengthValue:label控件
                lblLengthValue.Text  =  lPicLong.ToString() + "  KB " ;
                
// 顯示圖像尺寸 lblSizeValue:label控件
                lblSizeValue.Text  =  bmPic.Size.Width.ToString()  +   " × "   +  bmPic.Size.Height.ToString();
            }
        }
    }
}


你可能感兴趣的:(C#)