How to: Search for a Contact and get Contact Picture in Windows Phone
published on: 8/5/2011 | Views: N/A | Tags: Mango
by WindowsPhoneGeek
In this post I am going to talk about how to get Contact Picture via code in Windows Phone Mango.
Actually this is a common question that we have been asked recently, so we decided to answer it here.
Before we begin:
NOTE: Although Windows Phone Emulator contains sample contacts they do not have photos. So you should test using a physical device.
You can also take a look at our previous post: How to choose a Contact and get Contact details in a WP7 app
How to: Search for a Contact and get Contact Picture
With the Mango release you can now query the Contact List and retrieve information via the Contacts class. Here is how we can search for the target Contact using the SearchAsync() method (Note that FilterKind determines the kind of filtering you want, it can be PhoneNumber, DisplayName, EmailAddress, etc.):
private void btnSearchContact_Click(object sender, RoutedEventArgs e)
{
Contacts contacts = new Contacts();
contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
contacts.SearchAsync(displayName, FilterKind.DisplayName, null);
//search for all contacts
//contacts.SearchAsync(string.Empty, FilterKind.None, null);
}
NOTE: While in the code snippet above, we are not using the third parameter (named "state") of the SearchAsync method, it is good to know that it can be used to pass data to the SearchCompleted handler, as the value passed in the state parameter is stored in the ContactsSearchEventArgs.
Here is how we get the actual data using the Results property of ContactsSearchEventArgs in the SearchCompleted handler. Have in mind that in some cases there are more than one e-mails, for example, so in such cases we could use FirstOrDefault() to get the first available. We will get all pictures that match the search result using BitmapImage and the GetPicture() method. Next we will add them to the current page ContentPanel.
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
//alternatively you could use
//e.Results.FirstOrDefault() or e.Results.Last() instead to get only the first/last contact info
//find all pictures that match the search result
foreach (var result in e.Results)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(result.GetPicture());
Image img = new Image();
img.Source = bmp;
this.ContentPanel.Children.Add(img);
}
}
For reference you can also take a look at the Contact class MSDN Documentation.
That was all about how to get Contact Picture via code in Windows Phone Mango. Here is the sample project:
I hope that the tip was helpful.
You can also follow us on Twitter @winphonegeek
Comments
How to save image path
posted by: dinhvinh on 11/29/2011 12:58:32 PM
Hi Thank for this post. Can you help me how to save contact picture in Database using LINQ and reload it to Image control in a other Listbox? Thank
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
