2013-05-15 49 views
5

我想创建一个窗体,从文件中获取图像并显示在pictureBox中的c# 我有问题,当我在“=”后输入image.FromFile时,FromFile获取红色下划线为如果它不在图书馆。.FromFile下划线红色并显示错误

1 using System; 
2 using System.Collections.Generic; 
3 using System.ComponentModel; 
4 using System.Data; 
5 using System.Drawing; 
6 using System.Linq; 
7 using System.Text; 
8 using System.Windows.Forms; 
9 using System.IO; 
10 
11 namespace demo2 
12 { 
13  public partial class Image : Form 
14  { 
15  public Image() 
16   { 
17    InitializeComponent(); 
18   } 
19 
20   
21 
22   private void button1_Click(object sender, EventArgs e) 
23   { 
24    OpenFileDialog ofd = new OpenFileDialog(); 
25    ofd.Filter = "image files|*.png;*.jpg;*.gif"; 
26    DialogResult dr = ofd.ShowDialog(); 
27 
28    if (dr == DialogResult.Cancel) 
29     return; 
30 
31    pictureBox1.Image = Image.FromFile(ofd.FileName); 
32    textBox1.Text = ofd.FileName; 
33   } 
34       
35  } 
36 } 

回答

8

你的类被称为Image,这与系统冲突定义要使用Image。因此,当您尝试使用Image.FromFile时,编译器会使用您的名称空间中定义的那个(在您自己的类中),并且没有定义FromFile方法。

所以,当你要使用正确的Image类:

1),你应该有资格像命名空间:System.Drawing.Image.FromFile

2)您可以在自己的类重命名为从Image不同,所以你没有名字冲突

+0

你可以请编辑我的代码或告诉我哪里必须更改代码。 –

+1

第31行:'pictureBox1.Image = System.Drawing.Image.FromFile(ofd.FileName);' – sasjaq

+0

非常感谢亲爱的工作......非常感谢很多... :-) –

相关问题