New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading Bitmap using Relative File Path #2183
Comments
I don't think supporting this in the way you used it was intended. We need to introduce the file scheme to support loading assets from file. Currently the only way to load assets from file is in code. You could load the image in some viewmodel or in your code behind that should work. When you load the file from your application folder why don't you embed that file into your application and load it from resources? |
Considering the assembly has to be loaded into memory before it can be run, I think that's bad. Shipping the resource as a separate standalone file would be a better choice, especially for a large file. |
We might add Host handling for |
We used to bind Image controls to relative paths to files. This was important for us because users wanted to be able to exchange the files with their own ones (logos etc. for "branding" the software). Therefore I strongly vote for supporting files with relative paths again! |
I would suggest using a custom converter for this until this is implemented. |
Could you provide a short sketch or a link to some resource showing how to do this? |
Just implement IValueConverter that converts a string to Bitmap. This ctor needs to be called within the converter: Then use that converter in XAML. |
Ok, thanks. |
I would introduce static fields for the image paths and use them in a binding for the corresponding image control. https://stackoverflow.com/questions/42159000/xaml-converter-for-static-resource Just some idea how to solve this. A bit overhead but it should work until we can properly support file: scheme. Not sure if that works with Avalonia. With WPF this is supported:
|
Ok, I ended up with a mix of property and static resource binding. As a reference, here is my working demo code. Thanks again for the quick help!
And the converter class
|
Before #2104, it is able to use the relative path for Image
<Image Width="16" Height="16" Source="Images/File.png" />
(mak sure
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
first), where File.png is a Content that will copy to output directory. Currently, It throws a error of :
System.ArgumentException: Relative uris for 'resm' scheme aren't supported; resm:SampleApp.MainWindow.xaml?assembly=SampleApp uses resm
.However, it still is able to load the image with relative path by
Bitmap image = new Bitmap("Images/File.png");
The text was updated successfully, but these errors were encountered: