tags:

views:

491

answers:

2

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.

A: 

Try putting the return value of coDrivr4.Properties.Resources.Music.GetHbitmap() into a temp variable and see if it's null -- that may be where your null is coming from.

Jonathan
It's definitely not null, according to both the debugger and my quick in-code test.
Eric Smith
+1  A: 

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.

Jon Skeet
That's not it either. The same error throws on the 'object source =...' line.
Eric Smith
Right - but the important thing is that we've establish that it's definitely in ConvertFrom. Admittedly we'd have seen that immediately from the stack trace...
Jon Skeet
So what's the next step? I'm out of ideas for testing what's going wrong at this point.
Eric Smith
See my edit - try it under .NET 3.5. You might also want to try different resources, to see if that's particular file. If you can put it in a short but complete program, you could post the whole program here and the file, and we could try it ourselves.
Jon Skeet
Under .NET 3.5, the exception thrown is that ConvertFrom() can't convert from the IntPtr given by GetHbitmap() and it won't convert it from just the Bitmap object. Call me crazy, but I swear this has worked previously. I'm going to dig through some old code to see if I can find an instance of this working, that would have been the only reason I instinctively used GetHbitmap() in the first place.
Eric Smith
I'm getting the exact same problem, while it worked with older versions. Funny thing is, in VS008 with .NET 3.5 this worked, in VS2010 it does not and I'm sure the target isn't 4.0
TimothyP