confused with the use of System.Drawing.Image

G

Guest

I want to use the System.Drawing.Image class. According to the help file,
this is an abstract base class. Because it is supposedly abstract, I created
another class that inherits from it. However, when I did this I got the
following error:

'System.Drawing.Image.Image()' is inaccessible due to its protection level


Looking at other online examples, I found that the proper way to use it is
like this:

System.Drawing.Image Jpg = System.Drawing.Image.FromFile(PathToFile);

If this is an abstract base class, why can't I inherit from it and why am I
able to instantiate it as in the immediately above statement?
 
N

Nicholas Paldino [.NET/C# MVP]

Jerry,

You can inherit from it. However, we can't see how you have tried to
implement it, so we can't say what the problem might be. Can you post the
code of your implementation?
 
G

Guest

I define a class like this:

public class Image : System.Drawing.Image
{
public Image() {}
}

The error: 'System.Drawing.Image.Image()' is inaccessible due to its
protection level occurs if I do this:

Image myImage = new Image();


The other thing I don't understand is why I am able to do the following
without getting an error. Since it is abstract, shouldn't this not be allowed
because it instantiates an image object?

System.Drawing.Image Jpg = System.Drawing.Image.FromFile(PathToFile);

--
Jerry J


Nicholas Paldino said:
Jerry,

You can inherit from it. However, we can't see how you have tried to
implement it, so we can't say what the problem might be. Can you post the
code of your implementation?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jerry J said:
I want to use the System.Drawing.Image class. According to the help file,
this is an abstract base class. Because it is supposedly abstract, I
created
another class that inherits from it. However, when I did this I got the
following error:

'System.Drawing.Image.Image()' is inaccessible due to its protection level


Looking at other online examples, I found that the proper way to use it is
like this:

System.Drawing.Image Jpg = System.Drawing.Image.FromFile(PathToFile);

If this is an abstract base class, why can't I inherit from it and why am
I
able to instantiate it as in the immediately above statement?
 
B

Bruce Wood

Jerry said:
I define a class like this:

public class Image : System.Drawing.Image
{
public Image() {}
}

The error: 'System.Drawing.Image.Image()' is inaccessible due to its
protection level occurs if I do this:

Image myImage = new Image();


The other thing I don't understand is why I am able to do the following
without getting an error. Since it is abstract, shouldn't this not be allowed
because it instantiates an image object?

System.Drawing.Image Jpg = System.Drawing.Image.FromFile(PathToFile);

--
Jerry J


Nicholas Paldino said:
Jerry,

You can inherit from it. However, we can't see how you have tried to
implement it, so we can't say what the problem might be. Can you post the
code of your implementation?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jerry J said:
I want to use the System.Drawing.Image class. According to the help file,
this is an abstract base class. Because it is supposedly abstract, I
created
another class that inherits from it. However, when I did this I got the
following error:

'System.Drawing.Image.Image()' is inaccessible due to its protection level


Looking at other online examples, I found that the proper way to use it is
like this:

System.Drawing.Image Jpg = System.Drawing.Image.FromFile(PathToFile);

If this is an abstract base class, why can't I inherit from it and why am
I
able to instantiate it as in the immediately above statement?

You haven't instantiated it. You've called a static method, FromFile,
that returns a subclass of System.Drawing.Image, which you've assigned
to a base class reference (which is perfectly legal O-O). Try this:

System.Drawing.Image jpg = System.Drawing.Image.FromFile(PathToFile);
MessageBox.Show(String.Format("jpg type is {0}", jpg.GetType());

I think you'll see that the type is something other than "Image".
 
N

Nicholas Paldino [.NET/C# MVP]

Jerry,

You can't really derive from System.Drawing.Image. I was looking at the
class in reflector, and all the constructors are marked as internal (as is
the default one with no parameters), which means that you can't define an
implementation outside of System.Drawing.dll.

The reason why the call to FromFile works is that FromFile is returning
an instance of a derived class. You can access abstract types in your code.
The whole point of abstract classes is that they help provide some base
implementation details (otherwise, it would be the same as an interface if
it only defined methods which needed to be overridden) so that you have a
contract (the class definition of the abstract type) which you can access,
regardless of the derived type.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jerry J said:
I define a class like this:

public class Image : System.Drawing.Image
{
public Image() {}
}

The error: 'System.Drawing.Image.Image()' is inaccessible due to its
protection level occurs if I do this:

Image myImage = new Image();


The other thing I don't understand is why I am able to do the following
without getting an error. Since it is abstract, shouldn't this not be
allowed
because it instantiates an image object?

System.Drawing.Image Jpg = System.Drawing.Image.FromFile(PathToFile);

--
Jerry J


Nicholas Paldino said:
Jerry,

You can inherit from it. However, we can't see how you have tried to
implement it, so we can't say what the problem might be. Can you post
the
code of your implementation?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jerry J said:
I want to use the System.Drawing.Image class. According to the help
file,
this is an abstract base class. Because it is supposedly abstract, I
created
another class that inherits from it. However, when I did this I got the
following error:

'System.Drawing.Image.Image()' is inaccessible due to its protection
level


Looking at other online examples, I found that the proper way to use it
is
like this:

System.Drawing.Image Jpg = System.Drawing.Image.FromFile(PathToFile);

If this is an abstract base class, why can't I inherit from it and why
am
I
able to instantiate it as in the immediately above statement?
 
G

Guest

OK, I understand now. I do think the Microsoft Help is a bit unclear. Thank
you for the info Bruce and Nicholas.
--
Jerry J


Jerry J said:
I define a class like this:

public class Image : System.Drawing.Image
{
public Image() {}
}

The error: 'System.Drawing.Image.Image()' is inaccessible due to its
protection level occurs if I do this:

Image myImage = new Image();


The other thing I don't understand is why I am able to do the following
without getting an error. Since it is abstract, shouldn't this not be allowed
because it instantiates an image object?

System.Drawing.Image Jpg = System.Drawing.Image.FromFile(PathToFile);

--
Jerry J


Nicholas Paldino said:
Jerry,

You can inherit from it. However, we can't see how you have tried to
implement it, so we can't say what the problem might be. Can you post the
code of your implementation?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jerry J said:
I want to use the System.Drawing.Image class. According to the help file,
this is an abstract base class. Because it is supposedly abstract, I
created
another class that inherits from it. However, when I did this I got the
following error:

'System.Drawing.Image.Image()' is inaccessible due to its protection level


Looking at other online examples, I found that the proper way to use it is
like this:

System.Drawing.Image Jpg = System.Drawing.Image.FromFile(PathToFile);

If this is an abstract base class, why can't I inherit from it and why am
I
able to instantiate it as in the immediately above statement?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top