How to Save Image in Local Photos album of Windows Phone ?


Sometimes you may want to develop a windows phone app which containing many images to view like WallPaper app for Windows Phone LockScreen, Celebrity album, etc . It is quite easy to display images by using Image Control and passing source path to respective Image Control, but is it enough to just give functionality to see images only ? User always like to keep good images in their phone. so, In that condition user should have an app that can provide facility of save images also. This blog simply shows how you can write code that would allow user to save Image locally in their Save Image Photo album.

Implementation –

Here, you can see some piece of code which containing a function SaveImageToPhotoHub with few statements and a parameter type WriteableBitmap. This SaveImageToPhotoHub function returns either true or false output. To work with this function you just need to pass needed Image as parameter in SaveImageToPhotoHub function.

 private bool SaveImageToPhotoHub(WriteableBitmap bmp)
        {

            using (var mediaLibrary = new MediaLibrary())
            {
                using (var stream = new MemoryStream())
                {
                    var fileName = string.Format("Gs{0}.jpg", Guid.NewGuid());
                    bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
                    stream.Seek(0, SeekOrigin.Begin);
                    var picture = mediaLibrary.SavePicture(fileName, stream);
                    if (picture.Name.Contains(fileName)) return true;
                }
            }
            return false;
        }

WriteableBitmap provides a BitmapSource that can be written to and updated. To declare WritableBitmap class you must include System.Windows.Media.Imaging namespace

using System.Windows.Media.Imaging;

MediaLibrary is a class which is used to access songs, playlists, and pictures in the device’s media library. To acess MediaLibrary class you need to include Microsoft.Xna.Framework.Media namespace

using Microsoft.Xna.Framework.Media;

SaveJpeg is an Extentions method which is responsible to encodes a WriteableBitmap object into a JPEG stream, with parameters for setting the target width and height of the JPEG file.

SavePicture is a method of MediaLibrary class which saves the image contained in the stream object to the media library, and then returns that saved image as a picture object.

To acess MemoryStream class you need to include System.IO namespace.

using System.IO;

Now take a look how to use the above method for saving image.

private void Save_Click(object sender, EventArgs e)
        {
            WriteableBitmap bmp = new WriteableBitmap(imgview, null);
            if (SaveImageToPhotoHub(bmp))
            {
                MessageBox.Show("Image Saved", "Information", MessageBoxButton.OK);
            }
            else
            {
                MessageBox.Show("Error : Image Not Saved", "Information", MessageBoxButton.OK);
            }
        }

Here, imgView is an Image Control which is placed in xaml part


Note :- To run this code properly your app should have capabilities to access Windows Phone windows phone media library hub. You can simply do this by checking ID_CAP_MEDIALIB_PHOTO from capabilities tab of WMAppManifest.xml

Isn’t it so easy to learn how to save image locally on Windows Phone device. I hope you liked this article and this will help you while writing code for your app.

Advertisements

About Ravi Ranjan Kumar

An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.
This entry was posted in C#, CodeProject, Tips&Tricks, Windows Phone, Windows Phone App and tagged , , , , . Bookmark the permalink.

2 Responses to How to Save Image in Local Photos album of Windows Phone ?

  1. loius says:

    Thank for sharing this..
    just a question;
    is the imgview a component from the toolbox?

  2. loius says:

    I got it..
    Its supposed to be the name property of the Image you are saving
    Thanks again Ravi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s