NotABot
NotABot

Reputation: 526

Windows phone null image error

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

Upvotes: 1

Views: 137

Answers (2)

James
James

Reputation: 802

Looking at this quickly, couldn't you just do:

if (result.GetPicture() != null)
{
     bmp2.SetSource(result.GetPicture());
}
else
{
     bmp2.SetSource(Application.GetResourceStream(new Uri(@"Images/ci2.png", UriKind.Relative)).Stream);
}

If not, I have implemented a different solution for this and can probably post some more detail.

Upvotes: 5

Chubosaurus Software
Chubosaurus Software

Reputation: 8161

I see two problems.

<Ellipse.Fill>
    <ImageBrush Stretch="Fill" ImageSource="{Binding imageByte, Converter={StaticResource BytesToImageConverter}}"/>
</Ellipse.Fill>

You have a {Binding imageByte} but in the C# code you have

listobj.Add(new AddressBook()
{
    FirstName = result.DisplayName ?? "",
    imageBytes = AddressBook.imageConvert(bmp2),
    // ...
});

imageBytes != imageByte

Unless you're not showing the complete code.

Like the other poster said, set it from the resource file. His code will cause an error because it includes an extra "/" so change it to....

if (result.GetPicture() != null)
{
     bmp2.SetSource(result.GetPicture());
}
else
{
     bmp2.SetSource(Application.GetResourceStream(new Uri(@"Images/ci2.png", UriKind.Relative)).Stream);
}

Upvotes: 0

Related Questions