What is the best way to store static images (like toolbox icons) in a WPF app?
Right now I have Images directory and use them like this:
<dxb:BarButtonItem x:Name="rbSignOut" Content="Sign out" Glyph="Images/Icons/close-16x16.png" LargeGlyph="Images/Icons/close-32x32.png" />
I think it's not the best practice because when I move XAML file in to a subfolder, I need to change all paths. Also, it just does not seems right to store paths in code. So how do I do it properly?
Instead of your path, which is relative to the XAML file:
Glyph="Images/Icons/close-16x16.png"
Use a path which is relevant to the application root (using a leading forward slash)
Glyph="/Images/Icons/close-16x16.png"
No matter where your XAML file is, your image will always be referenced from the root. As long as you don't move your images, you'll always be fine.
Use a ResourceDictionary together with your images. You can add it to the generic.xaml ResourceDictionary so you'll only have to change one path if you move it and you can use the images in every xaml file.
Just Right-Click and change your image's "Build action" to "Content" and use like this
<Image Source="/WPFApplication1;component/Images/Image.png" />
I felt it as the best approach to use Images,Video etc as the Files are not embedded into the assembly.
Related
I want to access my folder that contains Image files.
The folder is located inside my project. Here is the look on my Explorer:
[]
I am not able to access it through code when I do the following: Vanilla_Icons_Installer.Images.
How can I access Image1.png inside Images folder by doing PreviewPicture.Image = path?
In WPF you can use a relative path inside your XAML like this:
<Image x:Key="Image" Source="../Images/Image1.png" />
But in your screenshot I see that you are using WinForms and you want to set it through code.
Therefore you could try this:
PreviewPicture.Image = Image.FromFile(
Path.Combine (
Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location),
"Images/Image1.png"));
Don't forget to set the Copy to Output directory option to e.x. "Copy if newer" for your Image file.
The above code will create a Images directory in your bin/debug folder with the image inside.
Actually this fixed my problem:
PreviewPicture.Image = Image.FromFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "LightColorful", "ClassImages.png"));
And setting the Copy to Output property of Image to Copy if newer.
I have a User Control with several images inside. I will set images sources from a main directory like "Website\Pictures".
My User Control is located in a specific directory like this: "Website\UControls" (Template Source Directory)
I'm trying to Load my user control inside a div in another page. Everything will work OK but images sources. I will see user control's Template Source Directory at the beginning of every image and it cause to have broken links.
For example an image source should be like this: "../Pictures/1.png", but I will have this: "../UControls/Pictures/1.png" where "UControls" is Template Source Directory.
How can I avoid such treating?
You should use ~ operator as a sign of the root your site.
See this answer as an example
getting base url of web site's root (absolute/relative url)
Seems my original answer is irrelevant to the problem.
Please try with leading slash as in https://msdn.microsoft.com/en-us/library/ms178116.aspx in the part
<img src="/Images/SampleImage.jpg" />
If the URL of your Web site is http://www.contoso.com, the path would resolve to the following:
http://www.contoso.com/Images/SampleImage.jpg
I tried to localize my Windows Universal App with the Multilingual app toolkit.
Because you can't bind strings directly from a .resw file in XAML, I used the ReswFileCodeGenerator tool. It works great in code behind, but in XAML I can't get this right.
In Windows Phone 8.0 I could use:
Text="{Binding Path=LocalizedResources.StringName, Source={StaticResource Strings}}"
Is there a similar way in Windows (Phone) 8.1 with the ReswFileGenerator tool?
I would still suggest you do it as Depechie already suggested: use x:Uid attribute.
If you need to get the localized string from code, just replace . in resource name with /. So, in XAML you'll write:
<TextBlock x:Uid="appString" />
In code you'll use:
var resourceLoader = new ResourceLoader();
var localizedText = resourceLoader.GetString("appString/Text");
When doing resource translations in wp8.1 with .resw files, you need to use the x:Uid attribute on your xaml control!
Like <TextBlock x:Uid="FieldKey" />
Details are mentioned here...
I've coded something to help me with this. It may not be perfect, but works great for me. Here's a link to the helper. You build it and put the .exe file in some easy-to-reach folder.
In the project with the resources, you set the pre-build action to something like this (you only need to change the "path\to\ResourcesHelper.exe" part):
call "path\to\ResourcesHelper.exe" "$(TargetName)" "$(ProjectDir)\" "$(RootNameSpace)" "universal"
Also, the main resources must be in Resources/en-US folder of your project (you can change it in the code, though).
This way, when you build the project, it will generate a file called LocalizedStrings.cs which is something like the file generated for .resx files. It contains some additional properties called LC (for lower case), UC (for upper case) and UCF (for upper case first) which return the strings in that casing. I hope you'll find it useful. :)
Note: The tool wasn't meant for other people, so I really just coded what I needed, so it may not work flawlessly.
I am setting an absolute path to Image control. This path is having spaces. After assigning the path to ImageURL property of the Image ASP.NET control, it does not show the image. I don't have option to remove the spaces as it is the requirement. Also, this path is outside the root directory(There is basically a FileUpload control that takes the file and then I am assigning the path to Image control).
Firstly is it possible to do. If yes how? Below are the code blocks relevant to the question
Server Code
target.ImageUrl = strImagePath;
where target is the Image control id
File Path: C:\Users\WebMaster\Downloads\2 States Full Vedio Songs 720p Bluray Encodded By(Khanz)\Screenshoot\vlcsnap-2014-05-17-13h22m13s103.png
Rendered HTML
<img id="target" alt="[Jcrop Example]" src="C:\Users\WebMaster\Downloads\2%20States%20Full%20Vedio%20Songs%20720p%20Bluray%20Encodded%20By(Khanz)\Screenshoot\vlcsnap-2014-05-17-13h22m13s103.png" />
Thanks in advance for your help.
Thanks Manson. I was so wrong. The image has to be hosted on the web server to access. Actually, this is what I want to cut down. Uploading the image from local user path to server folder was taking time. But I forgot the basics. Re-writing your comment as answer.
I have a program that the user can switch the program language on run time.
I store the current language used in Program Settings and I access it in my program using
Properties.Settings.Default.Language
Now In my xaml View files I want my buttons to change their ToolTips when the user change the current language.
I have two Rescorces files: EnglishRescource.resxandFrenchRescource.rex
And I bind the ToolTips of my buttons using
ToolTip="{Binding Path=NewDocument, Source={StaticResource Resources}}"
But I don't know if this is a correct approach. How can I bind to the correct Resource file when the language is switched.
EDIT
I renamed my resources files to Resources.En-US.resx and Resources.Fr-CA.resx
Is there any particular reason you are naming them {language}Resource.resx instead of the standard, Resource.{locale}.resx?
A few sites that might be of use
http://www.codeproject.com/KB/WPF/WPF_Resx_Localization.aspx
http://msdn.microsoft.com/en-us/library/ms788718.aspx