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
//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);
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With