In WinForms it is possible to import an image as a resource, and the image would still work when compiled in the /bin/Debug folder.
I can't figure out how to get this working in WPF, when I run the application the image doesn't load, because the image is saved in /Projects/AppName/images/, and the application is compiled into /Projects/AppName/bin/Debug when I run it in Debug mode.
Do I simply need to make a copy of my Images folder and put it where the application is compiled? Or is there another way. Here is my code which displays my image:
<Image Width="300">
<Image.Source>
<BitmapImage DecodePixelWidth="300" UriSource="/images/jamsnaps-dark.png" />
</Image.Source>
</Image>
Create a folder (e.g. images) in your Visual Studio Project.
Add the image file(s) to that folder.
Set their Build Action to Resource (in the Properties window, see second image in this answer).
Then write the UriSource property like you already did:
UriSource="/images/jamsnaps-dark.png"
That URI is effectively a Resource File Pack URI, where the prefix is automatically added by the XAML Parser.
In code behind, you would write
bitmap.UriSource = new Uri("pack://application:,,,/images/jamsnaps-dark.png");
Two options :
1) Go out from bin/Debug and in to your application folder by ../../ and then to your image.
<Image>
<Image.Source>
<BitmapImage UriSource="../../images/jamsnaps-dark.png" />
</Image.Source>
</Image>
2) Compile your images as Content from properties in the context menu on the image file , and then when compiled they would be placed in been debug.
I don't remember if you also need to tell them to copy local also in properties ( i'm not near a computer at the moment so i can't check .
Add the image to your project directory
In the solutions explorer, right click the image and select Include in Project
Build Action should be set to Resource by default.
You can then start to use the image path relative to the root e.g. images/text.jpg if you put it in a folder called images.
Related
When I try to display some smaller (72x72) images in my xaml file as a 20x20 image, they work fine, but when I try to use one that's 96x96, they just don't show up. Here is the code:
<Image Source="/Images.Toolbox;component/Images/defaultIcon.jpg" Height="20" Width="20" Stretch="None"/>
This is how it looks like with the 72x72, how I want it to look.
And this is how it looks when I try to use my 96x96 image.
The working one is a .ico, .png also works. The nonworking one is a .jpg, but this shouldn't matter should it?
Visual Studio > Solution Explorer
right-click on the image file > Properties
make sure the Build Action is set to Resource.
This problem is getting me crazy, I really have no clue what's going on - why it's not working as I think it should.
I have a WPF C# project. I've added a directory named "ExtFiles" to my solution. This directory contains a ICO file, a PNG image and TTF font.
All three files have "build action" set to "resource", and "copy to output dir" set to "Do not copy".
I call these 3 files in the XAML via:
For the Icon:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="Swiftech_Imperium.MainWindow"
Title="Imp" Height="435" Width="510" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" Icon="pack://application:,,,/ExtFiles/imp.ico"
For the Image:
<Image HorizontalAlignment="Left" Width="278" Height="30" Margin="9,401,0,0" VerticalAlignment="Top" Source="pack://application:,,,/ExtFiles/Logo.png"/>
And for the font:
<Label x:Name="LabelTemp1" BorderBrush="#FFFFA421" BorderThickness="1" FontFamily="pack://application:,,,/ExtFiles/#DS-Digital" Height="30"
Everything works, no error. The image is displaying, the icon too, the font is working.
But, when I publish, it does generate the ExtFile directory with the icon in it. Why is that? My goal is to have all 3 files embedded in the EXE. My goal is deploy the program using the single EXE file and nothing else, as the users's requirement.
All these 3 files are called only in the XAML and nowhere else, I don't understand why it's working fine for the image and font but not for the icon...
Any idea?
Thanks
Steve
Edit: I tried this: Embed multiple icons in WPF EXE (last answer) but it doesn't work.
Icon="/Resources/Imp.ico"
Source="/Resources/Logo.png"
FontFamily="/Resources/#DS-Digital"
I get this error (only for the icon, If I can the Icon=".." out it starts but the image and the font are not applied...
'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '5' and line position '9'.
I added the 3 files as resources (Resource, add file) and set their build action to "resource".
edit:
OK... I moved forward a tiny bit.
I added both DLL's (yeah there two!) to the resources.
Moved the image/icon/font to a separate folder (that probably wasn't needed).
but I was still getting the same error (parsing).
turns out it is my XAML that is calling the second DLL (which I originally forgot to add to the resources).
Now with both DLL's as resources, if I go into References and set both of these DLL's Copy Local to False, I get an error. I can put the first dll (WPFToolkit.dll) to false, but I can't do for system.windows.controls.datavisualization.toolkit.dll :(
reason why is because the XAML is calling it:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="Imperium.MainWindow"
Title="Imperium" Height="435" Width="510" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" Icon="pack://application:,,,/ExtFiles/Imp.ico" Closing="Window_Closing" MouseDown="Grid_MouseDown" WindowStyle="None" BorderThickness="1" BorderBrush="Black" Foreground="Black">
what can I do?
Your pack URN was wrong for the icon. It should be:
pack://application:,,,/(qualified assembly name i.e. My.Project.Executable);component/ExtFiles/imp.ico
When you add the file to the VS project folder you should set it as Resource with Do Not Copy.
The same applies for the image and the font - tho' I would also say that I've not loaded font using a direct reference like this and I'm not sure it works like this.
Try This:
Doing with Resources would be a different approach to achieve the same task. I would like to provide you a link to read certain msdn articles.
Adding and Editing Resources (Visual C#)
I have been trying to learn C# but I'm coming across a lot of problems. I am trying to display an image in WPF but for some reason, the image won't show! It appears on the Visual Studio editor but when I run the application it doesn't appear.
Here is some of my code:
This is how I'm trying to display the image:
<Image HorizontalAlignment="Left" Height="100" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="image.jpg"/>
I have also tried using this:
Source="pack://application:,,,/image.jpg"
Thanks for your help!
In your project:
Create a folder say "img", right click on it and select add existing item add image to folder
Go to properties of the added image, set Build Action as Resource.
It worked for me.
In XAML
<Image HorizontalAlignment="Left" Name="MyImg" Height="80" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="/img/Desert.jpg"/>
If none of these work, try changing the Build Action to Content.
That's what worked for me after struggling for a long time with this.
Go to the properties for the image in Visual Studio and change "Build Action" to "Resource" and "Copy to Output Directory" to "Copy if newer".
I had to do a rebuild, but then it worked. Cred to swapnil.
please drag the image to a image source,it will be like this /img/image.jpg"/
<Image HorizontalAlignment="Left" Height="100" Margin="273,147,0,0"
VerticalAlignment="Top" Width="100" Source="/img/image.jpg"/>
If none of the above works, try Rebuilding your application from the build menu. Not "Build", but "Rebuild"
For example, this is your project structure
+ProjectName
+--+imagesFolder
| +--amogus.png
|
+--App.xaml
+--MainWindow.xaml
+--etc.
and you want to access the to amogus.png in your xaml window,
You have two ways:
note this way the imagesFolder will be visible in the release build to users
to set amogus.png Build Action to Content and
Copy to Output Directory to Copy always more info,
then rebuild from the build menu, then add this to the window xaml
<Image Source="pack://siteoforigin:,,/imagesFolder/amogus.png" ></Image>
note this way the imagesFolder will be not visible in the release build to users
to set amogus.png Build Action to Resource and
Copy to Output Directory to Do not copy or blank more info,
then rebuild from the build menu, then add this to the window xaml
<Image Source="/imagesFolder/amogus.png" ></Image>
more detail
Right click images on the Solution Explorer, choose Properties and then set the Build Action as Resource.
Did not have to do a clean and rebuild. I tried every combination listed above (I am in VS2017)
Go to Project->Properties->Resources
Select File (drop down with choices of strings, images, icons...)
Click Add Resource->Existing File
Navigate to the image and import it
VS identifies it as an image (mine is PNG) and switches the view to show Image resources
Select the thumbnail of the image and in the Properties of the Image (type should be Bitmap) set Persistence to Embedded in resx
I saved and closed Project Properties as I got confused here before
Go to the Resources folder under the project and select the image (it should be listed for you)
Select the image and set the BuildAction to Embedded Resource
I set the File Action to Copy if Newer
From here I move back and forth between Debug and runtime, various combinations of clean, build and publish and the image has FINALLY been displayed every time.
Last tidbit, the XAML in the dialog looks like this:
<Image Source="pack://siteoforigin:,,,/Resources/DeathSpiral.png" />
I have updated several projects that were supposed to display graphics but didn't always do so using the steps above. They all work now. Tested in both VS2017 and VS2019 and no errors so far.
I had created a User Control in my main Project Originally. I have now created a new Project that uses the WPF User Control Library Template and moved my control there.
The issue I am having, is my control uses images as the content for buttons. I have moved the images along with the controls into the new project, but I cannot get the Pack URIs to work. The Control and the images both reside in the same folder of the project called MyControl an the new Project name is MyControls.
I have tried:
<Button Name="Button1" ToolTip="Button1" Click="Button1Action">
<Image Source="pack://application:,,,/MyControl/image1.png" />
</Button>
and
<Button Name="Button1" ToolTip="Button1" Click="Button1Action">
<Image Source="pack://application:,,,MyControls;/MyControl/image1.png" />
</Button>
I also tried adding the images to the Resources.resx file, then in the Code Behind converting that to a BitmapSource, creating an Image control, setting its source to the BitmapSource, and then setting the Button.Content to the Image. I assume that since the png has a transparent background, it was messed up in the process, it was displayed with a black background using this method.
The Build Action for the Image in the project is set to Resource, so I assume I am just missing the correct Pack string.
Your second pack uri was close, but you missed one word (and a slash)! It should be
<Image Source="pack://application:,,,/MyControls;component/MyControl/image1.png" />
In my c#/WPF project, I added a jpg to the resources to embed into the exe file.
Now I want to use this jpg in an image tag, something like
<xmlns:prop="clr-namespace:MyProgram.Properties"
<Image Source="{Binding Source={StaticResource prop:LogoJpg}}"
My problem: it does not work. I got no idea, how to use the image. I could use files from the hdd, but I need the image to be embedded in the exe file.
First, add the image to your project, with a Build Action of "Resource" (not EmbeddedResource). For instance, I've added an image called "logo.jpg" to a folder called Images in my main project.
Then, in XAML, you use just use that resource as follows:
<Image Source="Images\logo.jpg" />
You can also use the pack syntax for the source:
<Image Source="pack://siteoforigin:,,,/Images/logo.jpg" />
Hope this helps.