I have a module called xModule. I have an image inside it marked as embedded resource.
Inside the initialization of the module I tried :
string stFileName = "SmallIcon.png";
string stAssembly = "xModule"; // That's the full name of the assembly
//img.BeginInit();
Uri uri = new Uri(String.Format(#"/{0};component/Images/{1}", stAssembly , stFileName),
UriKind.Relative);
ImageSource imgSource = new BitmapImage(uri);
During it's coming to the breakpoint on the ImageSource imgSource =.. line, it seems the ImageSource can't find the image...
Why? What's wrong with it?
I get no errors, the assembly is referenced. I did something similar in the Xaml and it worked.
Also used : VS11, Unity
I think you need to mark the image as "ressource" not "embedded ressource" ( WPF )
also See : What's the difference between a Resource and an Embedded Resource in a C# application?
Related
I am trying to load an icon that is in my project directly in the main directory.
In order to do that i do this:
Dim uriSrc As Uri = New Uri("pack://ELE100WalkerWPF:,,,/assemblyName;Component/ShowMCF.png")
Dim bitmapImage As BitmapImage = New BitmapImage With {.UriSource = uriSrc}
Dim image As System.Windows.Controls.Image = New System.Windows.Controls.Image() With {.Source = bitmapImage}
This simply doesn't work even if for my styles i reference them in the same way:
<ResourceDictionary Source="pack://application:,,,/ELE100WalkerWPF;component/Resources/Colors.xaml" />
The only difference is that my styles are defined in the MergeDictionary in the application.xaml like this
<ResourceDictionary Source="Resources/Colors.xaml" />
Can somebody please explain why the image is not showing and how i can solve this? Thanks in advance
pack://ELE100WalkerWPF:... is not a valid Pack URI.
You can also not set a BitmapImage's UriSource property without calling BeginInit and EndInit. Use the BitmapImage constructor that takes an Uri argument.
Provided that ShowMCF.png is located in the top level folder of your Visual Studio Project, and its Build Action is set to Resource, this should work:
Dim uri As Uri = New Uri("pack://application:,,,/ShowMCF.png")
Dim bitmapImage As BitmapImage = New BitmapImage(uri)
If the image resource is in a referenced assembly (called ELE100WalkerWPF), you must included the assembly name like this:
Dim uri As Uri = New Uri("pack://application:,,,/ELE100WalkerWPF;component/ShowMCF.png")
Dim bitmapImage As BitmapImage = New BitmapImage(uri)
I have Class Library project that creates a dll that I side load into my main application dynamically. The main application contains API calls that I use and one of the calls is to load an icon image into a WPF button. I provide "pack://application:,,,/NamespaceOfMyDll;Component/Resources/embeddedresource.ico"
as my URI source and following is the code that tries to load this image
var logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(source);
logo.EndInit();
The image is an embedded resource of the dll that I am side loading. Doing this throws an exception saying the source is not found.
The project that is trying to load the image has no reference to the dll that I am side loading.
Is there a way to load the image without having to put it in the main application project?
Here's a workaround you can go with :
When you add images to the class's resource(I mean in Resources.resx file of the class), create public variables for each resource you add/you want to access. Sample :
public static class TestClass
{
public static Bitmap Image1 { get { return Resource1.Image1; } }
public static Bitmap Image2 { get { return Resource1.Image2; } }
}
Now, let's move on to loading the .dll into the main project :
Assembly Mydll = Assembly.Load("dll path here");
Type MyLoadClass = MyDALL.GetType("dllAssemblyName.ClassName");
object obj = Activator.CreateInstance(MyLoadClass);
Now, try to access the Bitmap variables :
Bitmap img1 = (Bitmap)obj.GetType().GetField("Image1").GetValue(obj);
///use the bitmap the way you want :)
Hope this helps
Using Build Action "Resource" instead of "Embedded Resource" solved the issue.
"Resource is for WPF applications when you want to use uri's to link to resources. Embedded resource is an embedded resource for WinForms applications that should be accessed via a ResourceManager."
https://social.msdn.microsoft.com/Forums/vstudio/en-US/29b6d203-18fb-40b0-a01f-d5b787ccf3be/build-action-resource-vs-embedded-resource?forum=netfxbcl
I am just learning c# and have been struggling to work with URIs in WPF. I've googled around a fair bit but not having much luck.
Essentially I'm trying to have a BitmapImage object stored as a property in a Car object. I then want to display the BitmapImage in an Image control on a WPF form.
The app is a simple app (it's for a Uni assignment), so no database, etc.
I have two methods of doing this. The first is that I'm preloading Car data from a text file, including the filename of the JPG I want to load. I have included the JPG in a directory called Files which is off the main directory where my source code and class files are. I have set the JPG file to 'Content' and 'Always copy'. When I run a Debug, it copies the Files directory and the JPG to the debug\bin directory.
My code creates a BitmapImage by referring to the JPG using a URI as follows;
BitmapImage myImage = new BitmapImage (new Uri("Files/" + Car.Imagefilename, UriKind.Relative);
Car.Image = myImage;
ImageControl.Source = myImage;
If I step through this code in the debugger, it sometimes works and displays the image, but most of the time it doesn't.
My second method is when a user creates a new Car. This method always works. In this one, I use a file dialog box (dlg) to select the image and use an absolute path.
BitmapImage myImage = new BitmapImage (new Uri(dlg.Filename, UriKind.Absolute);
Car.Image = myImage;
ImageControl.Source = myImage;
So....I can't work out why the first method doesn't work. I think it's got something to do with the relative reference, but I can't work out how to syntax that properly to work. I've tried using "pack:,,,", I've tried adding "component", I've tried an '#' before the "pack". I can't seem to find something that explains this simply.
Apologies if this is straight forward but it's doing my head in! Appreciate any pointers.
If the image files are located in a "Files" folder of your Visual Studio project, you should set their Build Action to Resource (and Copy to Output Directory to Do not copy), and load them by a Resource File Pack URI:
var image = new BitmapImage(new Uri("pack://application:,,,/Files/" + Car.Imagefilename));
Car.Image = image;
ImageControl.Source = image;
There is no need to copy the files anywhere. Images are loaded directly from the assembly.
First try to load the image file using its absolute path. For example if the images are stored in c:\projects\yourproject\files, then try using something like
BitmapImage myImage = new BitmapImage (new Uri("c:/projects/yourproject/files/carname.jpg", UriKind.Absolute);
If it works, what you are facing is an path calculation issue.
At this point you may either calculate the Absolute with reference to your executable using AppDomain.CurrentDomain.BaseDirectory at runtime or use App.Config to store the path and reference it from there.
Cheers
I am trying to write helper assembly with a helper class with a static method that loads Image from resource.
Here is the code
public static BitmapImage GetImageFromResource(Assembly assembly, string file)
{
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(#"pack://application:,,,/" + assembly.GetName().Name + #";component/" + file, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
return src;
}
I am trying to call this from a different assembly which has the png file with Build Action as Resource (Have also tried, Embedded Resource and Content)
I am getting the following error
Additional information: Cannot locate resource 'coffee.png'.
What am I doing wrong ? Also, the C is uppercase, but somehow in error message it appears that it is trying to locate coffee.png instead of Coffee.png.
Anybody knows what am I missing here ?
Make sure that the assembly that you are loading has the updated dll. Try doing a rebuild on your class library file that has the image. What you have should work. It is only the matter that the image is not really in the dll.
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!!
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
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.
You can use a tool such as Reflector to see the full names of the resources in the Assembly.
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.