Click here to Skip to main content
14,837,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a path to an image coming from DB in the form: Pictures/image_1.jpg in WPF and am using a converter.

and I'm trying to create a Uri where my current directory is; G:\Projects - Visual Studio\Stamps\Stamps\bin\debug

C#
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType == typeof(ImageSource))
            {
                if (value is string)
                {
                    string str = value.ToString();
                    Uri uri = null;

                    if (str == "None")
                    {
                        uri = new Uri(str, UriKind.RelativeOrAbsolute);
                        return new BitmapImage(new Uri("Pictures/NoImage.jpg", UriKind.RelativeOrAbsolute));
                    }
                    else
                    {
                        // This one does not work because my path has spaces
                        uri = new Uri(str, UriKind.RelativeOrAbsolute);
                        // The following works because it has no spaces in path name
                        //uri = new Uri("Z:/Zipped/" + str);
                        BitmapImage bmi = new BitmapImage();
                        bmi.BeginInit();
                        bmi.UriSource = uri;
                        bmi.EndInit();
                        return bmi;
                    }
                }
                else if (value is Uri)
                {
                    Uri uri = (Uri)value;
                    return new BitmapImage(uri);
                }
            }
            return value;
        }


then in xmal file I have;

XML
<Window.Resources>
    <con:ImageToSourceConverter x:Key="ImageSourceConverter"/>
</Window.Resources>

<DataGrid Name="dgUsers" AutoGenerateColumns="False"
          IsReadOnly="True"
          CanUserSortColumns="False"
          VerticalGridLinesBrush="Transparent"
          ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Scott #" Binding="{Binding [ScottNumber]}" />
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Width="42" Margin="0 5px" Source="{Binding Path=[ImagePath], Converter={StaticResource ImageSourceConverter}}"></Image>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="Description" Binding="{Binding [Description]}" />
    </DataGrid.Columns>
</DataGrid>


The path to the file is not found and no image is displayed when there are spaces in the path and when there are no spaces it works fine. So my question is now do I handle spaces in the path when creating Uri?

What I have tried:

I've also tried creating an absolute Uri and replacing spaces with "%20" and "+" and both caused an exception.
Posted
Comments
[no name] 21-Feb-21 7:57am
   
And why not BitmapImage bmi = new BitmapImage(uri);
Mike Hankey 21-Feb-21 8:03am
   
Tried that same result

1 solution

This link could solve your issue:

Uri.AbsoluteUri and UrlEncoding of Local File Urls

Uri.AbsoluteUri and UrlEncoding of Local File Urls - Rick Strahl's Web Log[^]
   
Comments
Mike Hankey 21-Feb-21 9:08am
   
You know I looked at that article and didn't think it pertained.

Works like a charm.
[no name] 21-Feb-21 9:44am
   
And can somebody enlighten also a monad like me what's the trick *blush*
Mike Hankey 21-Feb-21 9:47am
   
string str = value.ToString();
var baseUri = Environment.CurrentDirectory;
Uri uri = null;

if (str == "None")
{
// The secret is adding the file
uri = new Uri($"file:///{baseUri}" + "/Pictures/NoImage.jpg", UriKind.RelativeOrAbsolute);
bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = uri;
bmi.EndInit();
return bmi;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900