3

I have the following situation:

I have a Solution that is named MySolution, inside this solution there is some projects including a project named PdfReport. Inside this project there is a folder named Shared and inside this folder there is an header.jpg image.

Now I am trying to retrieve this file and I have found this code on the official documentation (http://msdn.microsoft.com/en-us/library/aa287676%28v=vs.71%29.aspx):

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
this.pictureBox1.Image = Image.FromStream(file);

On the documentation say that:

Replace "AssemblyName.ImageFile.jpg" with the name of the resource, as it is known in the assembly.

I am finding some difficulties to understand what I have to insert in my specific case as input paramether for the GetManifestResourceStream() method.

Can you help me to retrive my file?

Tnx

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • See [here](http://stackoverflow.com/questions/1192054/load-image-from-resources-area-of-project-in-c-sharp) – TaW Apr 13 '14 at 11:04

4 Answers4

2

//first steps 1. you addeed an imager to the project. 2. Right click on the image and change Build Action to Embedded Resource. Right click on the image and change Build Action to Embedded Resource.

In your code: uncomment the line below and add a breakpoint. It will return an array with all the resources listed. Simply find your resource, and replace the "assembly.draft.png" with your resource.

//  var d = thisExe.GetManifestResourceNames();
System.IO.Stream file =
 thisExe.GetManifestResourceStream(@"assembly.draft.png");
while ((read = file.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
wesreitz
  • 79
  • 6
0

GetManifestResourceStream takes the name that the resource is stored under in the assembly. The compiler picks that name. As far as I remember the name is pieced together from the assembly name, a namespace name and the file name.

Honestly, what I always do to find the name is to open the compiled assembly in Reflector or any other .NET disassembler and just copy the resource name from there. I admit that this is a primitive technique not based on understanding of the technology but it might serve you well.

usr
  • 168,620
  • 35
  • 240
  • 369
0

If it is an embedded resource, you can get it using the name MySolution.Properties.Resources.ImageFile.jpg, otherwise use a statement like below

this.pictureBox1.Image = Image.FromFile(Application.StartupPath + @"\shared\ImageFile.jpg");
CuriousPen
  • 249
  • 1
  • 8
0

Use method String[] fileNames = assembly.GetManifestResourceNames(); It will return all files including the namespaces, just take the one that ends with your name:

    public static Stream ExtractResourceFile(Assembly assembly, String fileName )
    {
        // get all embedded resource file names including namespace
        String[] fileNames = assembly.GetManifestResourceNames();

        String resourceName = null;
        String temp = "." + fileName.ToUpper();
        foreach (var item in fileNames)
            if (item.ToUpper().EndsWith(temp))
                resourceName = item;
        if (resourceName == null)
            throw new Exception("Embedded resource [" + fileName + "] not found");
        Tracer.Debug("Resource file name [{0}] found as [{1}]", fileName, resourceName);

        // get stream
        Stream stream = assembly.GetManifestResourceStream(resourceName);
            if (stream == null)
                throw new Exception("Embedded resource [" + resourceName + "] could not be opened.");
        return stream;
    }
SijeDeHaan
  • 175
  • 2
  • 2