Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageSourceConverter throws a NullReferenceException ... why?

Tags:

c#

resources

wpf

I've been tearing my hair out over this issue for the last hour or so.

I have some code that goes like this:

videoTile.Icon = new ImageSourceConverter().ConvertFrom(coDrivr4.Properties.Resources.Music.GetHbitmap()) as ImageSource;

When I run my code, it says a NullReferenceException occurred. Neither 'Music' nor the return of GetHbitmap() are null.

I'm trying to get the image via the Properties because it's the only way I've figured out how to access the images in my Resources folder. I would just add them to the app.xaml file as a resource, but I'm not using an app.xaml file for a few reasons.

Am I attempting this wrong? All I need to do is get an ImageSource object of an image I have in my Resource directory. I can use them just fine in my XAML, but can't for the life of me do it in any code.

P.S.: I can't just add them as a resource to the XAML file because this is just a class and so there is no XAML file.

like image 636
Eric Smith Avatar asked Jun 24 '09 05:06

Eric Smith


People also ask

Why am I getting a NullReferenceException?

The runtime throwing a NullReferenceException always means the same thing: you are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longer initialized). This means the reference is null , and you cannot access members (such as methods) through a null reference.


2 Answers

I hit exactly the same issue - I've got all my bitmaps in a nice, statically typed resource file and I just want to set an ImageSource with them. So, since the ImageSourceConverter was throwing null reference exceptions, I changed tack and used this piece of code instead:

Bitmap bitmap = entityCol.EntityCollectionImage;
this.Image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); // Image is an image source

Hope that helps.

like image 101
AdamCrawford Avatar answered Sep 18 '22 15:09

AdamCrawford


Well you've got plenty of things which could be null in there. I suggest you separate them out:

Bitmap bitmap = coDrivr4.Properties.Resources.Music;
object source = new ImageSourceConverter().ConvertFrom(bitmap.GetHbitmap());
ImageSource imageSource = (ImageSource) source;
videoTile.Icon = imageSource;

Note the use of a cast rather than the as operator here. If source isn't an ImageSource, this will throw an InvalidCastException which will be much more descriptive than just ending up as a null reference.

EDIT: Okay, so now we know for sure that it's happening in ConvertFrom, I suggest the next step is to find out whether it's a bug in .NET 4.0 beta 1. Are you actually using any .NET 4.0 features? I suggest you try to extract just that bit of code into a separate project (you don't need to display an API, just convert the image. Try to run that code in .NET 3.5. If it fails in the same way, that's eliminated the beta-ness from the list of possible problems.

like image 33
Jon Skeet Avatar answered Sep 22 '22 15:09

Jon Skeet