In a legacy project there are absolute path to the images,
for example:
C:/projects/LegacyProject/Project/Client/UserInterface/Images/arrow.png
Now I want to use relative path, so that every developer can use that
project, no matter where he has his copy of the sourcecode.
Is there an easy way to find out the (Resource) relative path?
How can I use it then?
At the moment I have for example:
<Image Source="C:/projects/LegacyProject/Project/Client/UserInterface/Images/arrow.png" Stretch="Fill" />
What I want is something like:
<Image Source="arrow.png" Stretch="Fill" />
Tried around with
<Image Source="pack:,,, arrow.png" Stretch="Fill" />
<Image Source="/WPF1;arrow.png"></Image>
and similar things
Put the image files into a folder (named let's say Images) in your Visual Studio project and set their build action to Resource.
Now you can simply use them in XAML like this:
<Image Source="Images/arrow.png" ... />
In code-behind you would have to write
var uri = new Uri("pack://application:,,,/Images/arrow.png");
image.Source = new BitmapImage(uri);
Add image using Source property of the Image control by clicking
then the path will be something like this:
/[project name];component/[folder name: e.g. Images]/[file name]
Related
is there a nice way to do the following. Get a WPF button and a Windows.Forms.ImageList and use them together. Here is the code:
<Button Name="SOMETHING" Content="Button" Height="23" VerticalAlignment="Top" Width="75"/>
System.Windows.Forms.ImageList imgList = new System.Windows.Forms.ImageList();
string str_directory = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
this.imgList.Images.Add(System.Drawing.Image.FromFile(str_directory + "\\Resources\\AboutImage.png"));
WPFbutton.Background = imgList.Images[0];
I am tyring to get the Windows.Forms ImageList and use it on a WPF button. Is there a nice way to fix it?
There is no need for an ImageList. Just do it as shown in the code below, which assumes that the path "Resources\AboutImage.png" is relative to the application's current working directory.
Apparently you've called Path.GetDirectoryName two times to cut off the "bin\Debug\" or "bin\Release\" part of the path and thus access the image file directly from the Visual Studio project structure. This will not work when the application is deployed somewhere else. Instead, set the Build Action of the image file to Content, and Copy to Output Directory to Copy always or Copy if newer.
var path = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "AboutImage.png");
var bitmap = new BitmapImage(new Uri(path));
WPFbutton.Background = new ImageBrush(bitmap);
However, a far better approach would be to load an image resource directly. Set the Build Action to Resource and load the image by a WPF Pack URI:
var bitmap = new BitmapImage(
new Uri("pack://application:,,,/Resources/AboutImage.png"));
WPFbutton.Background = new ImageBrush(bitmap);
Besides that, you would usually set the Background in XAML, like:
<Button ...>
<Button.Background>
<ImageBrush ImageSource="/Resources/AboutImage.png"/>
</Button.Background>
</Button>
I samples of FlowDocument in C# show images pointing to a location on disk in the xaml file. If I want to point source to image inside program as resource or how do i set the source setting then? I ship my program flowdocument as part of the program and I cant install images on disk. I want them as resource inside the program.
<InlineUIContainer >
<Image Margin="2,0" Width="50" Source="C:\sample.jpg" ></Image>
</InlineUIContainer>
You should set image's Build action to Resource and use following code:
<InlineUIContainer >
<Image Margin="2,0" Width="50" Source="pack://application:,,,/YourAssemblyName;Images/sample.jpg"></Image>
</InlineUIContainer>
sample.jpg must be in folder "Images" in your project in this example.
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.
I'm new to WPF, facing a problem in locating an image file on a button.
It's not working when I put a relative path, as shown(Window1.xaml):
<Button Height="23" HorizontalAlignment="Right" Margin="0,33,38,0" Name="button1" VerticalAlignment="Top" Width="24" Background="AliceBlue" OpacityMask="Cyan" Grid.Column="1" Click="button1_Click">
<Image Source="Folder-icon.png"></Image>
</Button>
However, it's working when i put an absolute path:
<Button Height="23" HorizontalAlignment="Right" Margin="0,33,38,0" Name="button1" VerticalAlignment="Top" Width="24" Background="AliceBlue" OpacityMask="Cyan" Grid.Column="1" Click="button1_Click">
<Image Source="D:\Folder-icon.png"></Image>
</Button>
I try to describe my folder structure in picture.
Hope someone can guide me to load the image to the button within the same workspace using relative path.
If you compare the Image.Source values for both cases, you will see in the cast that it does work the underlying Uri looks like:
file:///D:/Folder-icon.png
In the case where it does not work, the Image.Source value is null. The problem is that without the full path, WPF assumes it is a relative path to an embedded resource, not a file on disk.
This link provides a detailed description of URIs. But you'd need to use something like the following to use relative paths.
pack://siteoforigin:,,,/Folder-icon.png
One other note, the default path will be in the <Your Project Path\bin\Debug folder, not the <Your Property Path> folder.
if is because you are not giving it correct path .Here is sample
/EmailScrapperWpf;component/Images/SearchDog.gif
it is like /projectname;then path where your image is placed
i think in your case it should be
<Image Source="/WPF1;Folder-icon.png"></Image>
when you Select image from the properties when you select the Image and press F4 there source navigate to the image then see what path is displayed after selecting the image and you have to place that path in Source=""
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.