I have a wpf app,and got a image useful for codebehind which\'s location in the project is something like \"projectName\\images\\pp.png\" and its build action is \"Resource\"(No
Windows Forms - Get an image embedded resource:
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var stream= assembly.GetManifestResourceStream("YourAssemblyName.images.pp.png");
var image = Image.FromStream(stream);
Note:
"YourAssemblyName.images.pp.png"
is case sensitiveYourAssemblyName
put your assembly name, for example for my project, it is my project name and also my default namespace.GetManifestResourceStram
use the code below:Find embedded resource names:
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
assembly.GetManifestResourceNames()
.Where(x => x.EndsWith("pp.png")) //Comment this line to find all resource names
.ToList()
.ForEach(resource =>
{
MessageBox.Show(resource);
});
WPF - Get an Image Resource and Convert to System.Drawing.Image
var bitmapImage = new BitmapImage(new Uri(@"pack://application:,,,/"
+ Assembly.GetExecutingAssembly().GetName().Name
+ ";component/"
+ "images/pp.png", UriKind.Absolute));
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapImage)bitmapImage));
var stream = new MemoryStream();
encoder.Save(stream);
stream.Flush();
var image = new System.Drawing.Bitmap(stream);
Note:
Local Assembly Resource File
The pack URI for a resource file that is compiled into the local assembly uses the following authority and path:
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.Ext