1

I'm new with XAML/WPF and I don't know how to deal with this issue, I spend too much time into this, but I can't figure it out.

I have a class called Card with a property which "should" contain a source of an image.

public class Card
{
     public ImageSource image; // this is the only one which doesn't get a value..
}

I also have a class with a method which add information to the Collection of card like this:

public class deck
{
     public card[] = new card[51];
     public void FillCards()
     {
         while(I > something) //don't mind about the loop, because it's working fine
         {
             card[i] = new card()
             {   
                 name = this.name, //works
                 number = this.number, //works
                 //works etc...
                 image = new BitmapImage(new Uri(path, UriKind.Absolute)), //except this is not working
                 //Before you ask, yes the path is correct...
             };
         }   
     }
}

So in short, my question is:

Why can't I store BitmapImage in the ImageSource property,

What I'm basically doing is this: How can I have a property in a class to store an picture/image?

But I'm getting NullReferenceException.

Sorry for the bad English...

Community
  • 1
  • 1
user2875095
  • 13
  • 1
  • 3
  • When you get a NullReferenceException, you have to find out where it comes from. Looks like your path variable is null. Run your program in the debugger, set a breakpoint at the line where you assign `image` and check the variable values. And please note that `public ImageSource Image;` is not a property, but a public field. – Clemens Oct 13 '13 at 07:55
  • @"C:/cards/" + t + (idCounter + 1) + ".png". This is my path, I tested it out and it works on – user2875095 Oct 13 '13 at 10:14
  • Even if it's not the path variable, the exception is still thrown somewhere in your code. You have to find that by debugging. – Clemens Oct 13 '13 at 10:22
  • I have changed the ImageSource property to BitmapImage, and that worked fine, so I take that as a solution I guess... – user2875095 Oct 14 '13 at 20:20

1 Answers1

-1

There are a lot of ways to store a picture. If the property will be binding to an Image Control. Then you can use: byte[], BitmapImage, string(Uri) to store a picture. This is because those types can automatic converter a BitmapImage to ImageSource.

In this case, you just need to modify the ImageSource To BitmapImage.

By the way, The reason why you can't use ImageSource is the Constructor of ImageSource is internal, so you can't even create an ImageSource object.

So, that's all, try to use some other type.

public class Card
{
     // Try some other type, they all can be bind to Image.Source.
     public BitmapImage image; 
}

An related question.

Hope it helpful.

Community
  • 1
  • 1
johnson
  • 388
  • 5
  • 16
  • -1: "the Constructor of ImageSource is internal, so you can't even create an ImageSource object". Although this is true, it does in no way prevent you from declaring a property of type `ImageSource`. You usually assign a type derived from ImageSource to that property, e.g. `BitmapImage`. This is by the way exactly how the [`Source`](http://msdn.microsoft.com/en-us/library/system.windows.controls.image.source.aspx) property of the Image control is declared. – Clemens Oct 13 '13 at 08:42
  • This is also incorrect "This is because Image.Source can automatic converter a BitmapImage to ImageSource". Such a conversion is not necessary, as ImageSource is the base class of BitmapImage. This is very basic object-oriented programming stuff: you can use a reference to an object of type BitmapImage wherever a reference to an ImageSource is required. What you might mean by automatic conversion is that WPF has built-in type conversion from `string` or `Uri` or `byte[]` to `ImageSource`. – Clemens Oct 13 '13 at 09:19
  • Sorry for my mistakes. – johnson Oct 14 '13 at 01:16