Click here to Skip to main content
Click here to Skip to main content
Technical Blog

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

, 21 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This blog help you to learn how to save image locally in your windows phone device.

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 access 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.

 <Image x:Name="imgview" Source="/Assets/YourImage.jpg"  /> 

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

RaviRanjankr
Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer
 
An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.
 
He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..
 
He keep himself on the desk of his imagination, hanging around with some inceptions.
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralReg : Appreciation PinmemberMember 1124973121-Nov-14 1:53 
Questionsave image from database Pinmemberhongdida15-Apr-14 18:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 21 Mar 2014
Article Copyright 2014 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid