Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.FromFile underlined red and showing error

Tags:

c#

i want to create a windows form which takes image from file and displays in the pictureBox in c# i am having problem when i type image.FromFile after "=" the FromFile gets red underline as if it does not consists in the library.

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 }
like image 656
Haider Khattak Avatar asked May 15 '13 14:05

Haider Khattak


1 Answers

Your class is called Image, which is in collision with system defined Image you want to use. So when you try to use Image.FromFile, compiler uses that one defined in your namespace (in your own class) and there's no FromFile method defined.

So when you want to use correct Image class:

1) you should qualify the namespace like: System.Drawing.Image.FromFile

or

2) you can rename your own class to something different from Image, so you have no collision in name

like image 137
sasjaq Avatar answered Oct 25 '22 02:10

sasjaq