<Application.Resources>
<BitmapImage x:Key="MyImageSource" UriSource="C:\Users\nwrobinson\Pictures\skyskanlogo.jpg" />
</Application.Resources>
hello,
im having an issue with the way C# handles resources, i understand that the above code is a reference to a picture, but i assumed that when the program compiled, it would include the picture for when it runs on other computers. instead, it just crashes when the resource is not present in that location. this makes no sense to me, there has to be a way to include the picture in the program so it will run right?
the way it was explained to me, the application always has to have a photo to reference, so does that mean every time i give someone the application i have to send them the photo as well, or is there a way to have the photo wrapped into the application.
this is my first WPF so im not 100% sure what going on all the time, it might have just been something that was overlooked.
thanks for the help
What you want to do is to add the picture to your project and set its Build Action to Resource. Then you can change your UriSource to something like
UriSource="pack://application:,,,/YourProjectAssembly;component/DirectoryName/FolderName/ImageName"
When you look at the properties for the image in Visual Studio, you can select a Build Action. This can be one of many options, including copying the image to the build folder, embedding it as a resource, and others.
Depending on how you include the image in your project, you will need to use the Pack URI syntax to specify where the image can be found. It is not straightforward, but there are enough examples here (http://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx) for when the resources are embedded etc. that you should be able figure it out. It typically involves specifying the assembly in which the resource is located and a relative path, with a very specific assortment of punctuation interspersed.
Related
I don't know if I asked it right, but basically what happened is that I made a winform app which loads its image from the resource folder.
The problem is that when I build the project and get the exe and give it to a friend, he won't have that resource folder like I do, so he'll get an error saying missing file.
How can I somehow mix, or combine, or attach the image with my app?
You need to add it to the project by navigating to the Properties Window and going to the resource tab and adding the image from there.
Alternatively, from the PictureBox Control you can import resource from your computer.
if you don't need to update it in the future, compile your program with the image property build action set to embedded resource.
if you need to change it in the future compile with property build action set to content.
INTRO:
Good day dear coders! I took a look around and wasn't quite able to find answer to my simple question, tho some questions do answer my question, however they seem to be advanced for me to fathom so I'm printing here my simple situation.
QUESTION:
I want to change BackgroundImage or Image of PictureBox and thats how I am doing this:
PictureBox.Image = new Bitmap(#"C:\Users\Ailayna\documents\visual studio 2012\Projects\FormCritterTalking\FormCritterTalking\Character Pictures\CharacterNormal.png");
This picture located in one of my project folders and some how writing whole path to the picture makes no logical sense to me, since I have included all needed pictures to the project, in specific project's folder and I wonder is there a more efficient way of changing picture rather than specifying the whole path of where picture is located?
Is there a way to directly access my folder where my project pictures located using code, like for example: PictureBox.Image = FolderName.PictureName;
I would like to know how are you guys doing this in more efficient and neat way? And another thing is, do I always have to say "new Bitmap"? Can it be something else?
Is there a way to directly access my folder where my project pictures
located using code, like for example: PictureBox.Image =
FolderName.PictureName;
Yes, there is. It's called resources.
In your project select properties then chose resources. Add image.
Access it:
yourProjectName.Properties.Resources.imageName
However I'd recommend to use streams to access images. This is the right way, especially in case of bitmaps. Do not forget to dispose it afterwards. See my answer here how to do this.
I am not aware of any PictureBox control in WPF but if you are looking for the same in WPF You can do it like this in XAML:
<Image Source="/MyProject.UI.Common;component/Images/Cut.png"/>
Here your Image is located inside "Images" folder.
Disclaimer: Since you're loading a picture directly off the hard drive (and I'm assuming it's working) I'm guessing we're talking about a WinForms project.
There are a number of options you can choose from.
The simplest of which is probably getting the current application assembly folder and using that:
How do I get the path of the assembly the code is in?
var dir = AppDomain.CurrentDomain.BaseDirectory;
The images should be copied along with the executable. Then you can either use the code above to get the current directory OR you can try and rely on a relative path (from where the executable is located to the images).
Another option is to add the pictures as resources - these will be part of your application, but the procedure of reading them is a bit more complex.
All, the question is a basic one. I have been loading all .png icon that I use in a complex UI as a resource. This means that the 'Resource' folder contains approx. 100-150 small .pngs. This works but should I be doing this and if not what should I be doing?
Thanks for your time.
well basically, if you think the images will change then store them outside a resource file. If they're going to be static over the lifetime of the application (or its version) then a resource file is fine
I am working on a windows application that will need to be branded. The client will be selling this to other businesses, and needs a customized logo and name for each sale.
The client does not know how to use visual studio!
I think I need to write a packager app to inject custom logo and string resources into the executable. I am planning on using WPF. But since this is a critical requirement, I'd be willing to do it in winforms if that is easier.
What is the best way to do this? Any and all suggestions welcome.
It sounds like what you are after is application skinning. This doesn't mean you have to unpack the exe and inject resources. You just need to consider skinning from the start of the project and build the application to support your skinning requirements.
WPF will make skinning your app much easier. There will be several different ways to accomplish what you want.
Simplest is to leave the logo image loose and reference it with a relative path from the XAML file(s) that need to show this image.
You should look into Resource Dictionaries in WPF and how they help you group resources and support skinning. http://msdn.microsoft.com/en-us/library/ms750613.aspx
The text will be a little different but I am not sure what you need as far as a text goes. Do you mean you need to localize the strings or do you simply need different text (all the same locale) to show for different clients?
One possible solution (perhaps not the simplest one) is to use a parent application which compiles source code for generating child application. You can do it with CSharpCodeProvider and CompilerParameters classes. Add the image as an embedded resource and retrieve it in the child application. A working demo with a source code is available at Slide Show Builder.
My best suggestion for the exact question you asked (although I suspect there is another way by reconsidering the exact requirements) would be to write a utility which uses ildasm to disassemble the assembly, then use ilasm to reassemble it and include your new resource file.
http://msdn.microsoft.com/en-us/library/496e4ekx%28VS.71%29.aspx
The trivial solution is to provide the bitmap along with the EXE as a separate file. Actually replacing an embedded resource in the EXE requires decompiling it with ildasm.exe and putting it back together with ilasm.exe. Ildasm.exe is only available in the Windows SDK, it can be downloaded separately. Error prone and small odds that your customer can get that right, you'll need to provide them with, say, a .bat file that does this.
Of course, whomever is interested in replacing the logo, for whatever reason, would not be slowed down by replacing either the separate image file or using the Ildasm.exe trick. There is therefore very little point in making it any more complicated then it needs to be.
Alright, so I'm working on programming my own installer in C#, and what I'd like to do is something along the lines of put the files in the .exe, so I can do
File.Copy(file, filedir);
Or, if this isn't possible, is there another way of doing what I am attempting to do?
I wouldn't code my own installer, but if you truely want to embed files into your assembly you could use strongly typed resources. In the properties dialog of your project open up the "Resources" tab and then add your file. You'll then be able to get the file using:
ProjectNamespace.Properties.Resources.MyFile
Then you'll be able to write the embedded resource to disk using:
System.IO.File.WriteAllBytes(#"C:\MyFile.bin", ProjectNamespace.Properties.Resources.MyFile);
Honestly, I would suggest you NOT create your own installer. There are many many issues with creating installers. Even the big installer makers don't make their own actual installers anymore, they just create custom MSI packages.
Use Mirosoft Installer (MSI). It's the right thing to do. Make your own custom front-end for it, but don't recreate the already very complex wheel that exists.
UPDATE: If you're just doing this for learning, then I would shy away from thinking of it as "an installer". You might be tempted to take your "research" and use it someday, and frankly, that's how we end up with so many problems when new versions of Windows come out. People create their own wheels with assumptions that aren't valid.
What you're really trying to do is called "packaging", and you really have to become intimately familiar with the Executable PE format, because you're talking about changing the structure of the PE image on disk.
You can simulate it, to a point, with putting files in resources, but that's not really what installers, or self-extractors do.
Here's a link to Self-Extractor tutorial, but it's not in C#.
I don't know enough about the .NET PE requirements to know if you can do this in with a managed code executable or not.
UPDATE2: This is probably more of what you're looking for, it embeds files in the resource, but as I said, it's not really the way professional installers or self-extractors do it. I think there are various limitations on what you can embed as resources. But here's the like to a Self-Extractor Demo written in C#.
I'm guessing here, but if you are trying to store resources in your application before compilation, you can in the Project Explorer, right click a file you would like to add, chose properties and change the type to Embedded Resource.
You can then access the embedded resources later by using the instructions from this KB:
http://support.microsoft.com/kb/319292
in case you simply want to store multiple files in a single file storage (and extract files from there, interact etc.) you might also want to check out NFileStorage, a .net file storage. written in 100% .NET C# with all sources included. It also comes with a command line interpreter that allows interaction from the command line.