ShareMediaTask on Windows Phone 8

By Fons Sonnemans, 03-dec-2012

The new Windows Phone 8 SDK has a new ShareMediaTask class which can be used to share your pictures from code. I wanted to use this class to share a WriteableBitmap. You can use it to share the picture using NFC (Tap+Send), apps or to social media like Twitter or Facebook. In this post I will explain how to implement this.

Demo App

I have created a demo app in which a Rectangle and a Ellipse are drawn inside the default ContentPanel grid. When you tap the Share button the ContentPanel is used as the input for a WriteableBitmap. This WriteableBitmap is than saved so it can be shared using the ShareMediaTask().


The XAML is really basic.

<Grid x:Name="LayoutRoot"
        <RowDefinition Height="Auto" />
        <RowDefinition />
        <RowDefinition Height="Auto" />

    <StackPanel x:Name="TitlePanel"
        <TextBlock Text="SHARE MEDIA DEMO"
                    Style="{StaticResource PhoneTextNormalStyle}"
                    Margin="12,0" />
        <TextBlock Text="mainpage"
                    Style="{StaticResource PhoneTextTitle1Style}" />

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel"
        <Rectangle Fill="Blue"
                    Width="192" />
        <Ellipse Fill="#FF42B51B"
                  Width="226" />
    <Button Content="Share"
            Click="ButtonShare_Click" />


The code

In the ButtonShare_Click() method a screenshot of the ContentPanel is created using a WriteableBitmap. This WriteableBitmap is then saved to the MediaLibrary. The Path of the ShareMediaTask object is set using the GetPath() extension method on the picture class. The Microsoft.Xna.Framework.Media.PhoneExtensions namespace is brought into scope using a 'using' statement in the header of the class. Finally I called the Show() method on the ShareMediaTask object.

using Microsoft.Xna.Framework.Media.PhoneExtensions;


private void ButtonShare_Click(object sender, RoutedEventArgs e) {
    var bmp = new WriteableBitmap(this.ContentPanel, null);
    var width = (int)bmp.PixelWidth;
    var height = (int)bmp.PixelHeight;
    using (var ms = new MemoryStream(width * height * 4)) {
        bmp.SaveJpeg(ms, width, height, 0, 100);
        ms.Seek(0, SeekOrigin.Begin);
        var lib = new MediaLibrary();
        var picture = lib.SavePicture(string.Format("test.jpg"), ms);

        var task = new ShareMediaTask();

        task.FilePath = picture.GetPath();


I have also tried to save the WriteableBitmap to the IsolatedStorage. But the ShareMediaTask couldn't cope with that (bummer). I hope Microsoft will fix that in a future version of the SDK.

I really want to thank Clemens Schotte for solving this part of the puzzle.

Closure and download

I hope you like my solution. You can download my code here.



Leave a Comment

Leave a Comment
5 + 7 =


  • Aly Chiman
    23 feb 2019 03:56
    Hello there, My name is Aly and I would like to know if you would have any interest to have your website here at promoted as a resource on our blog ? We are in the midst of updating our broken link resources to include current and up to date resources for our readers. Our resource links are manually approved allowing us to mark a link as a do-follow link as well . If you may be interested please in being included as a resource on our blog, please let me know. Thanks, Aly
  • Mudassir
    20 jun 2014 09:43
    picture.GetPath(); is not working for me please help
  • Perihan
    15 mei 2014 01:34
    Thank you so much. It is very useful!
  • Kasper Mathiesen
    1 feb 2014 10:33
    Thanks for this, was the best written guide I found...
  • donfitz
    16 jan 2014 07:01
    Thanks for writing this. It worked flawlessly. You are a gentleman and a scholar.
  • sonali
    30 jul 2013 08:41
    thanx this really helped
  • sonali
    30 jul 2013 08:37
    I got my mistake.i did not add ID_CAP_MEDIALIB_PHOTO capability
  • sonali
    30 jul 2013 08:04
    this exception occurs on the line:var picture = lib.SavePicture(string.Format("test.jpg"), ms); An exception of type 'System.UnauthorizedAccessException' occurred in but was not handled in user code.
  • sonali
    30 jul 2013 07:57
    I have used the same code but I am getting unauthorised access exeption on the line: var picture = lib.SavePicture(string.Format("test.jpg"), ms);
  • Apoorv Kumar Upadhyay
    4 feb 2013 10:51
    I implemented this in one of my image editing applications . I wish to ask does the same code helps on sharing on social networks connected to the phone?

All postings/content on this blog are provided "AS IS" with no warranties, and confer no rights. All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors. The content on this site is licensed under a Creative Commons Attribution By license.