3

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!!

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Thuto Paul Gaotingwe
  • 1,425
  • 4
  • 15
  • 15

4 Answers4

6

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

Esi
  • 389
  • 5
  • 14
3

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.

Yann Duran
  • 3,842
  • 1
  • 24
  • 24
  • 'System.Windows.Media.Imaging.BitmapImage(); Assembly CurrentAssembly; CurrentAssembly = Assembly.GetExecutingAssembly(); Stream EmbeddedResourceStream = CurrentAssembly.GetManifestResourceStream(Application.Current.Details.Name+ "ElectricalContructors.Resource.logo.jpg"); if (EmbeddedResourceStream != null) { image.SetSource(EmbeddedResourceStream); }' **@Yann I double checked my stream it was still null** – Thuto Paul Gaotingwe Feb 18 '12 at 09:28
  • Is the resource in the same project your code is running (that's the other thing that can be wrong)? – Yann Duran Feb 20 '12 at 04:15
  • yah i think so..Yann, my code is in the UserCode dir and my Resources are in the Resources dir. All under client in my Lightswitch App.. I tried LR__'s code which takes the image from a URL and it worked! I just don't understand why this one...don't you have like a working code that you tested and reads from your Resources... maybe it could help? --thanx!! – Thuto Paul Gaotingwe Feb 20 '12 at 14:24
  • OK, two things are making this harder for me to answer. The code that I use is in an extension method that I know "just works", and I use VB, not C#. The method that I use requires a "Resource", not an "Embedded Resource". I'll put the C# code translation of my GetResourceUri method in answer above, as I don't seem to be able to sucessfully post code in a comment. – Yann Duran Feb 21 '12 at 00:19
0

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.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Tony Reynolds
  • 121
  • 1
  • 2
0

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

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76