2011-08-20 45 views
0

我對WinForms中的PictureBox中加載圖像有個疑問。
我想從我的表單上的PictureBox文件系統中顯示一個圖像文件,例如form1如何在WinForm中的PictureBox中的文件系統中顯示圖像

我在做使用C#的Windows應用程序。

我想檢查文件類型,也就是說它是pdf /文本/ png/gif/jpeg。
是否有可能以編程方式從使用C#的文件系統打開文件?
如果有人知道,請給出任何想法或示例代碼來做到這一點。

修改代碼:我已經這樣做了我的系統中打開一個文件,但我不知道如何安裝文件和附加的文件。

private void button1_Click(object sender, EventArgs e) 
{  
     string filepath = @"D:\"; 

    openFileDialog1.Filter = "Image Files (*.jpg)|*.jpg|(*.png)|*.png|(*.gif)|*.gif|(*.jpeg)|*.jpeg|"; 
    openFileDialog1.CheckFileExists = true; 
    openFileDialog1.CheckPathExists = true; 

    if (openFileDialog1.ShowDialog(this) == DialogResult.OK) 
    { 
     try 
     { 


     }   
    } 
} 

我不知道我要寫什麼在try塊。任何人都可以幫忙嗎?

+0

上傳到什麼?一個網頁?服務?一個文件服務器?電子郵件? – Tridus

+0

我想從我的系統上傳圖像,我想顯示上傳的圖像在我的表單圖片框.....喜歡文件上傳控制在asp.net web應用程序..如果我點擊按鈕一個文件將打開在我的系統......這樣的.. – user903550

+0

任何一個可以請幫我有點示例代碼.... – user903550

回答

2

使用Image.ImageFromFile http://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile.aspx方法

圖像IMG = Image.ImageFromFile(openFileDialog1.FileName);

應該工作。

編輯

如果你打算將它設置爲圖片框,看什麼裏面完成,利用PictureBox

SizeMode財產。

+0

thanq狄格蘭其現在的工作,但我想對圖像進行壓縮,因爲我無法看到完整的圖像.. – user903550

+0

你要去哪裏展示它? – Tigran

+0

在form1中設置的圖片框... – user903550

0
 using System.IO; 

     openFileDialog1.FilterIndex = 1; 
     openFileDialog1.Multiselect = false;  //not allow multiline selection at the file selection level 
     openFileDialog1.Title = "Open Data file"; //define the name of openfileDialog 
     openFileDialog1.InitialDirectory = @"Desktop"; //define the initial directory 


     if (openFileDialog1.ShowDialog(this) == DialogResult.OK) 
     { 
      try 
      { 
       string filename = openFileDialog1.FileName; 
       FileStream fs=new FileStream(filename, FileMode.Open, FileAccess.Read); //set file stream 
       Byte[] bindata=new byte[Convert.ToInt32(fs.Length)]; 
       fs.Read(bindata, 0, Convert.ToInt32(fs.Length)); 
       MemoryStream stream = new MemoryStream(bindata);//load picture 
       stream.Position = 0; 
       pictureBox1.Image = Image.FromStream(stream); 
      }   
     } 
相關問題