Thuto Paul Gaotingwe
Thuto Paul Gaotingwe

Reputation: 1425

GetManifestResourceStream giving Null Reference exception ( Parameter name: streamSource)

I keep on getting a null reference exception from GetManifestResourceStream, am trying to add Logo image to the Lightswitch ribbon and it is supposed to work just fine....

was referring to LR__ http://social.msdn.microsoft.com/Forums/en-US/lightswitch/thread/2d16c638-f833-4c4c-beec-656912a87b8e/#76fa5382-0135-41ba-967c-02efc3f8c3a2

System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
image.SetSource(Assembly.GetExecutingAssembly().GetManifestResourceStream(
    Application.Current.Details.Name + ".Resources.logo.jpg"));
Image myImage = new Image()
{
    Source = image,
    Stretch = System.Windows.Media.Stretch.Uniform,
    HorizontalAlignment = HorizontalAlignment.Left,
    Margin = new Thickness(2, 2, 2, 14),
    Cursor = System.Windows.Input.Cursors.Hand
};

I tried a lot of things but I can't find my where the problem is!!

Upvotes: 3

Views: 5652

Answers (4)

Esi
Esi

Reputation: 395

add your image or file in your project then select your file on the solution explorer window then on the properties window select Build Action then set value "Embedded Resource" to Build Action properties

just this

enter image description here

Upvotes: 6

Tony Reynolds
Tony Reynolds

Reputation: 121

I had this null return problem & I was tearing my hair out because this using image files as embedded resources is one of my standard tricks.

I eventually found the reason was that I'd been sent files from a graphic designer who uses an Apple & they didn't work. I fiddled with permissions; used a Paint program to save them in a different format but nothing worked. In the end I created a completely new file in Paint, then copied & pasted the pixels from the original image. It then worked.

Just for the record, does anyone know why this happened? It must be in the header blocks somehow.

Upvotes: 0

Yann Duran
Yann Duran

Reputation: 3879

Does Logo.jpg have it's build action set to "Embedded Resource"?

Edit:

Here the C# translation of my GetResourceUri (note, it needs a Resource, not an Embedded Resource):

public Uri GetResourceUri(this Assembly asm, string resourceName)
{
    Uri result = null;
    var assemblyName = new AssemblyName(asm.FullName).Name;

    result = new Uri(string.Format("/{0};component/{1}", assemblyName, resourceName), UriKind.Relative);

    return result;
}

The same "technique" should work in C#.

I also have a custom shell extension (that uses LR's technique to add images to both the ribbon & the navigation menu). I'm just finishing a few things (writing the "documentation" is taking some time) & then I'll release it on the Visual Studio Gallery for the community to use (it's called Luminous Classic Shell).

The extension allows you to have the images without needing to write code.

Upvotes: 3

Bernard Vander Beken
Bernard Vander Beken

Reputation: 5056

You can use a tool such as Reflector to see the full names of the resources in the Assembly.

Upvotes: 0

Related Questions