2

I am trying to decode a Tiff image using the code provided in the Microsoft Documentation:

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.tiffbitmapdecoder(v=vs.110).aspx

However, when I get to this line:

Image myImage = new Image();

I get an error telling me that I cannot create an instance of an abstract class. I already knew you cannot create an instance of an abstract class, I'm wondering why is this in the documentation?

Here is the whole block of code that is causing problems:

Image myImage = new Image();
myImage.Source = bitmapSource;
myImage.Stretch = Stretch.None;
myImage.Margin = new Thickness(20);

It also says that System.Drawing.Image does not contain a definition for .Source, .Stretch and .Margin which got me thinking I need to use a different namespace? So when I looked up the documentation for Image.Source:

http://msdn.microsoft.com/en-us/library/system.drawing.image(v=vs.110).aspx

It says it uses the System.Windows.Controls namespace which of course is a namespace that contains classes defined in PresentationFramework.dll - which I already have.

Any ideas what I am missing?

4
  • 6
    You're using the wrong class. Make sure you're using Image from System.Windows.Control, and not the one from System.Drawing.Image. To ensure this, one way would be to just prefix the class with the full namespace, ie. it would be this: System.Windows.Controls.Image myImage = new System.Windows.Controls.Image(); Commented Nov 5, 2014 at 13:29
  • Thank you very much for the response Lasse, much appreciated. Commented Nov 5, 2014 at 13:35
  • @LasseV.Karlsen Hey so I was looking through my questions and I realised I forgot to add an answer to this question. I remembered what you suggested worked for me so if you'd like to submit your comment as an answer, I'd be glad to accept it for you! Commented Nov 15, 2016 at 16:15
  • Posted a fuller answer instead of the comment as per request :) Commented Nov 15, 2016 at 16:34

2 Answers 2

4

There is probably ambiguous reference conflict between System.Windows.Controls.Image and System.Drawing.Image. Try removing System.Drawing.dll reference if you are not using it or just use fully qualified class name (namespace + class name)

Sign up to request clarification or add additional context in comments.

Comments

2

The documentation - which you linked to - contains, as you say, this line of code:

Image myImage = new Image();

for which you say you get this compiler error:

Cannot create an instance of the abstract class or interface

This means you're using the wrong Image class.

In the .NET framework and surrounding core libraries, there's only 2 such Image classes:

The first is used by the framework as the base class for image decoding and encoding, which would look promising for your question, except that it is a true base class, so it is abstract, which explains the error message you got.

The other class is used by the presentation framework, WPF, to represent an image on screen. This class, however, is not abstract.

So very likely you're using the wrong class here and have pulled in the System.Drawing.Image class whereas you really want to use the System.Windows.Controls.Image class.

There are several ways to fix this problem:

  1. Add a using directive to your file:

    using System.Windows.Controls;
    

    However, this will likely just replace the existing compiler error with another one, namely

    'Image' is an ambiguous reference between 'System.Drawing.Image' and 'System.Windows.Controls.Image'

    To get the existing code to figure out that you're using the wrong class you likely already have using System.Drawing;. If you can remove that, great! Otherwise, ...

  2. Add a using alias to specifically control which Image class you're talking about:

    using Image = System.Windows.Controls.Image;
    

    This may work, and then again may not. If it does, great! Otherwise, ...

  3. Prefix the usage of the class with the full namespace:

    System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
    

1 Comment

Wow a really excellent answer! Very thorough! Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.