I'm working on a app for windows phone, which is basically includes phone contacts. I'm getting all the phone contacts using Contacts class and I'm storing contact data in the isolated storage. Since I cant serialize images, I'm converting them to byte[] before serializing. My code is:
foreach (var result in e.Results)
{
if (result.PhoneNumbers.FirstOrDefault() != null)
{
BitmapImage bmp2 = new BitmapImage();
bmp2.SetSource(result.GetPicture());
listobj.Add(new AddressBook()
{
FirstName = result.DisplayName ?? "",
imageBytes = AddressBook.imageConvert(bmp2),
EmailAddress = "",
LastName = "",
Phone = result.PhoneNumbers.FirstOrDefault().PhoneNumber ?? "",
});
}
}
When Contact has no picture it shows an Argument null exception error on the line:
bmp2.SetSource(result.GetPicture());
So when contact image is null I want to use some custom image ("/Images/ci2.png" or any Blank image would also work). My xaml code is:
<StackPanel Margin="0,0,0,2" Orientation="Horizontal">
<StackPanel Width="80" Orientation="Horizontal" Height="80">
<Ellipse Margin="0" Height="70" Width="70" HorizontalAlignment="Left" Stroke="{x:Null}">
<Ellipse.Fill>
<ImageBrush Stretch="Fill" ImageSource="{Binding imageByte, Converter={StaticResource BytesToImageConverter}}"/>
</Ellipse.Fill>
</Ellipse>
</StackPanel>
<StackPanel Height="80" Margin="0" Width="380" HorizontalAlignment="Left">
<TextBlock FontWeight="Bold" Text="{Binding FirstName}" FontFamily="Segoe WP Semibold" FontSize="30" VerticalAlignment="Top" Margin="5,0,0,0" HorizontalAlignment="Left" />
<TextBlock Text="{Binding Phone}" FontFamily="Segoe WP" FontSize="24" Margin="5,0,0,-12" Width="320" HorizontalAlignment="Left" VerticalAlignment="Top">
<TextBlock.Foreground>
<SolidColorBrush Color="#FFCFC9C9"/>
</TextBlock.Foreground></TextBlock>
</StackPanel>
</StackPanel>
My question is, How can I use custom image when
bmp2.SetSource(result.GetPicture());
is null? Thanks