Google How to display the photo of a contact from Windows Phone C# | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Thursday 27 February 2014

How to display the photo of a contact from Windows Phone C#

Introduction

In some time we may need to get phone contact list images and then display in our images.So that we need to know about Contact object,which contains all available information about an individual contact.However it is a process of create a new bitmap image, set its source to GetPicture, and then display the bitmap in the user interface with reference to Contact object.

Source File at :GetContactImagewp8

Building the Sample

If you want to access Contact  in your app, you must specify the following capabilities in the app manifest. Otherwise, your app might not work correctly or it might exit unexpectedly.

ID_CAP_CONTACTSWindows Phone 8 
ID_CAP_APPOINTMENTSWindows Phone 8
Note: Windows Phone Emulator contains sample contacts; however, they do not have photos. You should test this procedure using a physical device (or) you may add contact photo to test in your emulator .

Description

 Instead of create a new bitmap image, set its source to GetPicture, and then display the bitmap in the user interface. I am going to use following custom contact converter class

C#
 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 == nullreturn 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(); 
        } 
    }

 1)Reference For above custom contact converter:

XAML
xmlns:MyContact="clr-namespace:ContactImage" 
    <phone:PhoneApplicationPage.Resources        <MyContact:ContactPictureConverter x:Key="ContactPictureConverter" /> 
    </phone:PhoneApplicationPage.Resources>
 Note: Don't forgot to mention public keyword for above class,otherwise you may get xamlparseexception occurred at initializecomponent().

      so that finally we have to use above converter like
XAML
 <Image Source="{Binding Converter={StaticResource ContactPictureConverter}}" Width="108" Height="108" Stretch="Fill" />

 2)UI for displaying contact list images:


XAML
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"            <StackPanel Orientation="Vertical" 
                <Button Content="Get Contact Pictures" Width="Auto" Click="GetContactPictures_Click" /> 
 
                <ListBox Grid.Row="1" Name="ContactResultsData" HorizontalAlignment="Left" ItemsSource="{Binding}" Height="570" Margin="24,24,0,0" VerticalAlignment="Top"                    <ListBox.ItemTemplate                        <DataTemplate                            <StackPanel Orientation="Horizontal"                                <Border Margin="0,10,0,0" BorderThickness="2" HorizontalAlignment="Left" BorderBrush="{StaticResource PhoneAccentBrush}"                                     <Image Source="{Binding Converter={StaticResource ContactPictureConverter}}" Width="108" Height="108" Stretch="Fill" /> 
                                </Border> 
                                <TextBlock Margin="10,10,0,0" Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" /> 
                            </StackPanel> 
                        </DataTemplate> 
                    </ListBox.ItemTemplate> 
                </ListBox> 
            </StackPanel> 
        </Grid> 

 3)Binding contact list images to ListBox:

C#
private void GetContactPictures_Click(object sender, RoutedEventArgs e) 
        { 
            Contacts cons = new Contacts(); 
 
            //Identify the method that runs after the asynchronous search completes. 
            cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted_Many); 
 
            //Start the asynchronous search. 
            cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #3 Picture"); 
        } 
 
        void Contacts_SearchCompleted_Many(object sender, ContactsSearchEventArgs e) 
        { 
            try 
            { 
                //Bind the results to the list box that displays them in the UI. 
                ContactResultsData.DataContext = e.Results; 
            } 
            catch (System.Exception) 
            { 
                //No results 
            } 
        }

 4)ScreenShots:


If you want to working with longlistselector ,please see this ConatcImagewithLongList 


Related Posts:
  1. WindowsPhone 8 LongListSelector: Scrollable List Items With Fixed Header's(C#-XAML)
Have a nice day by 

No comments:

Post a Comment

Search Engine Submission - AddMe