Sharing Image Files as Contextual Data in Lync 2010 Conversations: Code Listing (Part 2 of 2)

Summary:   This article describes how to share large files, such as images, in Microsoft Lync 2010 contextual conversations. To view a video demonstration, see Lync Contextual Data Part 3: Passing an Image File.

Applies to:   Microsoft Lync 2010 | Microsoft Lync 2010 SDK

Published:   January 2012 | Provided by:   John Clarkson, Microsoft | About the Author

Contents

Watch video  See video

Sending Application Code Listing

This section includes XAML and C# code for the sending application described in part 1.

Page.xaml

<UserControl x:Class="SenderApplication1.Page"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="https://schemas.microsoft.com/expression/blend/2008" xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:controls="clr-namespace:Microsoft.Lync.Controls;assembly=Microsoft.Lync.Controls" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">
        <Button Click="button1_Click"  Content="Button" Height="23" HorizontalAlignment="Left" Margin="50,53,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="33,114,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        <controls:StartInstantMessagingButton Height="50" Width="50" Name="IMButton" Margin="0,-12,0,12" />
    
    </Grid>
</UserControl>

Page.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Lync.Model.Conversation;
using Microsoft.Lync.Controls;

namespace SenderApplication1
{
    public partial class Page : UserControl
    {
        ConversationContextualInfo contextInfo;
        string imageData;
        string AppID = "{1226271D-64C9-4F24-B416-E6A583F45A1C}";
        Conversation conversation;

        public Page()
        {
            InitializeComponent();
            SendInitialData();
        }

        void SendInitialData()
        {
            contextInfo = new ConversationContextualInfo();
            contextInfo.ApplicationId = AppID;
            contextInfo.ApplicationData = "initial context data";
            IMButton.ContextualInformation = contextInfo;
            IMButton.Source = "barbara@contoso.com";
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            bool? result = ofd.ShowDialog();
            if (!result.HasValue || result.Value == false)
                return;
            FileStream filestream = ofd.File.OpenRead();
            byte[] binaryData = new Byte[filestream.Length];
            long bytesRead = filestream.Read(binaryData, 0, (int)filestream.Length);
            imageData = Convert.ToBase64String(binaryData, 0, binaryData.Length);
            conversation = (Conversation)Microsoft.Lync.Model.LyncClient.GetHostingConversation();
            conversation.BeginSendContextData(AppID, "text/plain", imageData, SendDataCallback, null);
            textBox1.Text = imageData.Length.ToString();
        }

        public void SendDataCallback(IAsyncResult res)
        {
            conversation.EndSendContextData(res);
        }
    }
}

Receiving Application Code Listing

This section includes XAML and C# code for the target application described in part 1.

Page.xaml

<UserControl x:Class="ReceiverApplication1.Page"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="https://schemas.microsoft.com/expression/blend/2008" xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:controls="clr-namespace:Microsoft.Lync.Controls;assembly=Microsoft.Lync.Controls" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">
        <Button Click="button1_Click" Content="Button" Height="23" HorizontalAlignment="Left" Margin="41,16,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="26,65,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        <Image Height="251" HorizontalAlignment="Left" Margin="131,122,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="340" />
    </Grid>
</UserControl>

Page.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Lync.Model.Conversation;
using System.Windows.Media.Imaging;

namespace ReceiverApplication1
{
    public partial class Page : UserControl
    {
        Conversation conversation;
        string AppID = "{1226271D-64C9-4F24-B416-E6A583F45A1C}";

        public Page()
        {
            InitializeComponent();
            conversation = (Conversation)Microsoft.Lync.Model.LyncClient.GetHostingConversation();
            conversation.ContextDataReceived += new EventHandler<ContextEventArgs>(conversation_ContextDataReceived);
        }

        void conversation_ContextDataReceived(object sender, ContextEventArgs e)
        {
            string data = e.ContextData;
            string ID = e.ApplicationId;
            string type = e.ContextDataType;
            textBox1.Text = type + " " + data.Length.ToString();
            byte[] imageBytes = Convert.FromBase64String(data);
            using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
            {
                BitmapImage im = new BitmapImage();
                im.SetSource(ms);
                image1.Source = im;
                image1.Stretch = Stretch.Uniform;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string data = conversation.GetApplicationData(AppID);
            textBox1.Text = data;
        }
    }
}

Part 1

Sharing Image Files as Contextual Data in Lync 2010 Conversations: Lync Applications (Part 1 of 2)

Conclusion

Contextual data must be a type string, and is limited in size. To satisfy these two requirements, convert the image to a string and then re-assemble the image file at the receiving end of the conversation.

Additional Resources

For more information, see the following resources:

About the Author

John Clarkson is a programming writer with the Microsoft Lync product team.