XAML Image source - c#

I'm writing a WPF app with VS2015. I have a User Control that contains an Image control. I have my image on the file system in an Images subfolder. In the Design mode, the image showed up fine, but when I ran it, it didn't. I specified the image location thus:
Source="Images/ball.png"
within the Image tag. I tried all the obvious things, like copying the folder to the bin\Debug folder and such. Experimenting, this error message popped up over the Source tag:
"Could not find a part of the path 'C:\Program Files (x86)\New folder\Microsoft Visual Studio 14.0\Common7\IDE\Images\red-ball.png'"
I finally got it to show up by giving it the FULL PATH to the image, but I really don't think I should have to. Isn't there a way to give it a relative path that's relative to the application and not the location of the IDE?

First, make sure that your Images subfolder is relative to your project ie:
Project
Images
ball.png
Then with "ball.png" added to your project, set its Build Action to "Resource". Your link will now work as you expect.
I'm guessing its looking for the image in the place you mentioned because it couldn't find it in the resources where it wants to look. Note that sounds and video clips do not work this way, you have to copy those to your build output or use a pack URI.

Related

how to set image in Visual Studio C# Windows Forms with Image.FromFile

So I'm trying to set a custom image for a form application I've made. The images I'm trying to target are in a folder called "Images" on the same level as my Solution file. The solution file is a C# windows forms (net core framework) solution. It's a basic form app that I want to display an image based on a users selection, however right now I get an unhandled exception everytime I try to set the image with this code:
picFood.Image = Image.FromFile("../../Images/burger.jpg");
The exact error is "System.IO.FileNotFoundException: ../../Images/burger.jpg"
In another totally unrelated solution this works. Folder structure is the same. A folder called Images, on the same directory level as the .sln file holds the images there. They're in my solution explorer and everything. I've tried this with one "../" and no "../" as well so I'm not sure what to do from here.
Files with relative paths are opened relative to the working directory of your application.
In this case, when launching from within Visual Studio, the default is the bin folder where the compiled application is put by default.
So if your binary is in <project dir>/bin/Debug/App.exe this path will resolve to <project dir>/Image/burger.jpg.
If you have changed something in your build configuration, or your application switches directory at runtime (e.g. via Directory.SetCurrentDirectory), this path may be different than you expect.
To understand your issue, I suggest you start looking at what your working directory is. You can obtain that in your code via Directory.GetCurrentDirectory().
You can also resolve your relative path using Path.GetFullPath.
Print these two values to see where your program attempts to load the file from.
Keep in mind that any image files you put in the solution/project folder will need to be copied with your binary if you want to use them.
To use relative paths without .. you can copy them alongside your binary during compilation, see:
VS2010 How to include files in project, to copy them to build output directory automatically during build or publish and Copying Visual Studio project file(s) to output directory during build for how to do that.

Image not found after ClickOnce publish

I am creating a PDF document, using MigraDoc. The image is located in the root folder of the project, so I use this code to insert it in the PDF-document:
Image img = tfLogoDate.AddImage("../../db_logo_500p.png");
The Build Action is set to 'Content':
This works fine during development, using the Visual Studio debug build. But when I create a build with ClickOnce (publish), it says that the image is not found.
Am I using the wrong Build Action? Or is it because the relative path changes, when I use ClickOnce to publish my application?
I found a way to make it work, by changing the image location. I now use this instead:
// I have added the 'pack://(...)' in front of the path
Image img = tfLogoDate.AddImage("pack://application:,,,/../../db_logo_500p.png");
I had to use the following settings for the PNG-file:
Build Action: Content
Copy to Output Directory: Do not copy
If anyone knows exactly why the above works, I would be delighted to know. But the most important thing is, that it works...

WPF application. How to reference image in xaml code, so another user can view them when running .exe file of the project?

I'm having a problem with my wpf application. I have two images in the xaml code, and when I run the application on my computer, they appear well, but when I ask someone to run the .exe file, the application run but the images don't appear. How can I set the images source so that when another user run the .exe file, the images appear in the screen?
The xaml image source code:
Source="\\MULINFSV0005\User$\mathias.deitos\Meus Documentos\Visual Studio 2013\Projects\WpfApplication2\WpfApplication2\clock.png"
If an image is in the project folder so probably the problem is in it's Properties.
Build Action: Resource
Copy to Output Directory: Do not copy
In xaml something like this <Image Source="../../Images/yorImage.png"/> The path to the image from the place where you're (mean from the current file to the image file. If both files are in the same directory Source="image.png"
This should work.

Not able to reference Image source with relative path in xaml

I have created a ClassLibrary project, and added a xaml of Window type.
I wrote a console application and showing this wpf window.
The problem is I have to show an Icon in this window.
If I am using following code it is not working
<Image Source="../Images/Folder-icon.png"></Image>
When I give some hard coded path like
<Image Source="E:\MyApp\Images\Folder-icon.png"></Image>
it works.
Why I have to give an hard coded path value? Is there any solution?
Hope I gave enough information. Please help I am struggling with this problem for more that 2 hours.
If Images folder is added in same project of its usage, this should work for you:
<Image Source="/Images/Folder-icon.png"/>
Or use Pack Uri specifically:
<Image Source="pack://application:,,,/Images/Folder-icon.png"/>
In case image resides in different project than current project where your XAML resides, you have to use more verbose definition of Pack URI where you have to specify the assembly name where image is added.
<Image Source="pack://application:,,,/Assembly;component/Images/Folder-icon.png"/>
Replace Assembly with actual assembly name where image is added into.
Also make sure Build Action is set to Resource for file Folder-icon.png.
UPDATE:
More verbose definition of Pack URI works in sample as well. Try this out:
<Image Source="pack://application:,,,/ClassLibrary1;component/Folder-icon.png"/>
Answers to such questions are often confusing because the principles/prerequisites for the solution to work are not explained correctly. This is the case with the currently selected answer too. So it's a good thing to be aware of:
The different ways to manage and reference a file/resource added to the project.
How to specify in the markup the URL for a file managed as Content.
The non-required use of the pack URI instead of the simple URL
Things to consider:
1. How to declare the type of file/resource you are adding to your project.
This is done by adding appropriate folder and files to the project using the Solution Explorer in VS, e.g. in a subfolder Images:
and specifying properties for each added file (on the right side below):
The Build Action is set to Content and the Copy To Output Directory is set to Copy if newer. This is how files should be added when you don't want them to be embedded into the assembly.
For reference, the Content option is one of the possible options to access a file in WPF. Other options include Resource (either embedded in the current assembly, or in another assembly of the same application) and Site of Origin to access a file which has no relationship with the current application:
Source
Content type files are linked to the executable (assembly) only by the relative path they are stored. The relative URL is stored in assembly metadata. These files are expected to be found at execution time, but they can be changed/updated without recompiling the assembly.
The Copy if Newer value means the compiler will take care of copying the original file to the right place during the assembly building process, so that the relative path is preserved, e.g. if a file is located in the subfolder Images of the project, e.g. MyProject/Images then a copy will be created in MyProject/bin/Debug/Images when the application is compiled in debug mode. The assembly being built in MyProject/bin/Debug, the relative path is the same.
2. How to specify a Content file URL in the markup.
This is the easy part, and usually the only one explained. But it works only if the previous step has been done correctly.
To use a content file, just use the markup shortcut of the relative path (relative to the assembly): <Image Source="Images/Lighthouse.jpg"/> (there is no leading /).
Other cases, where the file is not declared as Content type (e.g. an image which must be embedded into the assembly as a Resource, or a file which must be loaded from the network) involve syntax variations with the simple URL replaced by a pack URI.
3. Non-required / excessive use of the pack URI.
As the URI scheme can be used to reference anything, the pack URI solution is often proposed to also reference files declared as Content. For our example, the equivalent pack URI would be: pack://application:,,,/Images/Lighthouse.jpg (note the leading /).
However the URL shortcut is more appropriate, and less error-prone for a standalone file which location relative to the assembly is known at design time.
For a more detailed view see:
WPF Application Resource, Content, and Data Files
I got this working by:
Name of my WPF User Control project: WpfControls
Name of the folder containing the image: Images
Name of the imgae file: Asdf.png
Include the folder and image file into the WpfControls project
The image file's Build Action set to Resource
The Source property in the Image tag set to:
<Image Source=/WpfControls;Component/Images/Asdf.png" />

image load Visual Studio 2010 c#

I'm having problem loading images to my project.
I have the image in a folder named microassig this folder is on my desktop, this folder is to be sent online to my tutor, thats my line of code:
private Image imageOpen = Image.FromFile("\microassig\openOff.bmp");
I don't want to put the directory c:/ because is directory will be different from mine hence why i'm just using the ("\microassig\openOff.bmp");
The problem is that the image doesn't load.
Locate your project's
bin\debug folder
You can do this by right-clicking on your project solution and clicking Open in Windows Explorer.
Store/Save the file as
openOff.bmp
Then you can just do:
private Image imageOpen = Image.FromFile("openOff.bmp");
You should not be using the Image.FromFile. You need to add the image as a resource into the project. See this link: http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx
The \ doesn't imply your desktop. You should use GetFolderPath for that (See C# Get Special Folder)
There are a few ways to go about this. You could add your image as a resource.
Or, more in the line of what you are already doing you could change your code to something like this:
private Image imageOpen = Image.FromFile("openOff.bmp");
Then move the openOff.bmp to your bin folder (where the exe is saved).
While you keep the openOff.bmp in the same folder as your executable it should find it.

Categories

Resources