Contact.GetPicture Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Gets a picture of the contact.
Namespace: Microsoft.Phone.UserData
Assembly: Microsoft.Phone (in Microsoft.Phone.dll)
Syntax
Public Function GetPicture As Stream
public Stream GetPicture()
Return Value
Type: System.IO..::.Stream
A stream that contains the data for the picture.
Examples
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 8.
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.
}
}
Private Sub Contacts_SearchCompleted(sender As Object, e As ContactsSearchEventArgs)
Try
'We are using only the first contact.
Dim con As Contact = e.Results.First()
Dim img As BitmapImage = new BitmapImage()
img.SetSource(con.GetPicture())
Picture.Source = img
Catch ex As System.Exception
'We can't get a picture of the contact.
End Try
End Sub
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 8.
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();
}
}
Public Class ContactPictureConverter
Implements System.Windows.Data.IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object _
Implements System.Windows.Data.IValueConverter.Convert
Dim c as Contact = CType(value, Contact)
If c Is Nothing Then
Return Nothing
End If
Dim imageStream as System.IO.Stream = c.GetPicture()
If imageStream IsNot Nothing Then
Return Microsoft.Phone.PictureDecoder.DecodeJpeg(imageStream)
End If
Return Nothing
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object _
Implements System.Windows.Data.IValueConverter.ConvertBack
throw new NotImplementedException()
End Function
End Class
Version Information
Windows Phone OS
Supported in: 8.1, 8.0, 7.1
Platforms
Windows Phone
See Also
Reference
Microsoft.Phone.UserData Namespace
Other Resources
Contacts and Calendar for Windows Phone 8