This topic has not yet been rated - Rate this topic

Contact.GetPicture Method

July 26, 2012

Gets a picture of the contact.

Namespace:  Microsoft.Phone.UserData
Assembly:  Microsoft.Phone (in Microsoft.Phone.dll)
public Stream GetPicture()

Return Value

Type: System.IO.Stream
A stream that contains the data for the picture.

The following example assumes that you have a Windows Phone application that has a page with an image control named Picture. For the full example, including the XAML, see How to: Display the Photo of a Contact for Windows Phone.

void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
    try
    {
        //We are using only the first contact.
        Contact con = e.Results.First();

        BitmapImage img = new BitmapImage();
        img.SetSource(con.GetPicture());
        Picture.Source = img;
    }
    catch (Exception)
    {
        //We can't get a picture of the contact.
    }
}

The following example creates a data converter that you can use to data-bind contact photos directly to the UI. For the full example, including the XAML, see How to: Display the Photo of a Contact for Windows Phone.

public class ContactPictureConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Contact c = value as Contact;
        if (c == null) return null;

        System.IO.Stream imageStream = c.GetPicture();
        if (null != imageStream)
        {
            return Microsoft.Phone.PictureDecoder.DecodeJpeg(imageStream);
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Windows Phone OS

Supported in: 7.1

Windows Phone

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.