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); 
      }   
     } 
相关问题