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
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
{
uri = new Uri(str, UriKind.RelativeOrAbsolute);
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;
<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.