C#中使用picturebox按比例显示图片

 C#中使用picturebox按比例显示图片  
C#代码
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Text;   
  7. using System.Windows.Forms;   
  8. using System.IO;   
  9.   
  10. namespace WindowsApplication2   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         public Form1()   
  15.         {   
  16.             InitializeComponent();   
  17.             pcbPic.DoubleClick += new EventHandler(pcbPic_DoubleClick);   
  18.         }   
  19.   
  20.         void pcbPic_DoubleClick(object sender, EventArgs e)   
  21.         {   
  22.             OpenFileDialog ofdPic = new OpenFileDialog();   
  23.             //取得或設定目前的檔名擴展名,以決定出現在對話方塊中[檔案類型] 的選項。   
  24.             ofdPic.Filter = "JPG(*.JPG;*.JPEG);gif文件(*.GIF)|*.jpg;*.jpeg;*.gif";   
  25.             //取得或設定檔案對話方塊中目前所選取之篩選條件的索引   
  26.             ofdPic.FilterIndex = 1;   
  27.             //關閉對話框,還原當前的目錄   
  28.             ofdPic.RestoreDirectory = true;   
  29.             //取得或設定含有檔案對話方塊中所選文件的名稱。   
  30.             ofdPic.FileName = "";   
  31.             if (ofdPic.ShowDialog() == DialogResult.OK)   
  32.             {   
  33.                 //得到文件名及路徑   
  34.                 string sPicPaht = ofdPic.FileName.ToString();   
  35.   
  36.                 //FileInfo:提供建立、複製、刪除、移動和開啟檔案的執行個體 (Instance) 方法   
  37.                 FileInfo fiPicInfo = new FileInfo(sPicPaht);   
  38.                 //Length:取得目前檔案的大小。以字節為單位   
  39.                 long lPicLong = fiPicInfo.Length / 1024;   
  40.                 //得到文名   
  41.                 string sPicName = fiPicInfo.Name;   
  42.                 //取得父目錄   
  43.                 string sPicDirectory = fiPicInfo.Directory.ToString();   
  44.                 //DirectoryName :取得表示目錄完整路徑。   
  45.                 string sPicDirectoryPath = fiPicInfo.DirectoryName;   
  46.   
  47.     
  48.   
  49.                 //封裝GDI+點陣圖像,是用來處理像素資料所定義影像的物件。   
  50.                 //Bitmap類:封裝GDI+ 點陣圖,這個點陣圖是由圖形影像的像素資料及其屬性所組成。Bitmap 是用來處理像素資料所定義影像的物件。   
  51.                 Bitmap bmPic = new Bitmap(sPicPaht);   
  52.   
  53.                 //如果文件大於500KB,警告   
  54.                 if (lPicLong > 4000)   
  55.                 {   
  56.                     MessageBox.Show("此文件大小為" + lPicLong + "K;已超過最大限制的400K范圍!");   
  57.                 }   
  58.                 else  
  59.                 {   
  60.                     Point ptLoction = new Point(bmPic.Size);   
  61.                     if (ptLoction.X > pcbPic.Size.Width || ptLoction.Y > pcbPic.Size.Height)   
  62.                     {   
  63.                         //圖像框的停靠方式   
  64.                         //pcbPic.Dock = DockStyle.Fill;   
  65.   
  66.                         //圖像充滿圖像框,並且圖像維持比例   
  67.                         pcbPic.SizeMode = PictureBoxSizeMode.Zoom;   
  68.                     }   
  69.                     else  
  70.                     {   
  71.                         //圖像在圖像框置中   
  72.                         pcbPic.SizeMode = PictureBoxSizeMode.CenterImage;   
  73.                     }   
  74.                 }   
  75.                 //LoadAsync:非同步載入圖像   
  76.                 pcbPic.LoadAsync(sPicPaht);   
  77.   
  78.                 ////顯示圖像名 lblNameValue:label控件   
  79.                 //lblNameValue.Text = sPicName;   
  80.                 ////顯示圖像大小  lblLengthValue:label控件   
  81.                 //lblLengthValue.Text = lPicLong.ToString() + " KB";   
  82.                 ////顯示圖像尺寸 lblSizeValue:label控件   
  83.                 //lblSizeValue.Text = bmPic.Size.Width.ToString() + "×" + bmPic.Size.Height.ToString();   
  84.             }   
  85.         }   
  86.     }   
  87.   
  88. }   
posted @ 2008-11-22 20:43  冯赞锋  阅读(11139)  评论(0编辑  收藏  举报