Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a JPG image using GetManifestResourceStream() method?

Tags:

c#

.net

asp.net

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

like image 631
AndreaNobili Avatar asked Sep 03 '25 17:09

AndreaNobili


2 Answers

//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);
}
like image 70
wesreitz Avatar answered Sep 05 '25 06:09

wesreitz


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;
    }
like image 39
SijeDeHaan Avatar answered Sep 05 '25 06:09

SijeDeHaan