In this article I will demonstrate how to save image in Windows Phone 7. We can save image to cameraroll or picture album. I will use SavePictureToCameraRoll and SavePicture method of MediaLibrary.
Lets write code to implement saving image in cameraroll or picture album.
Step 1: Add reference of Microsoft.Xna.Framework
Step 2: Include below using directive in MainPage.xaml.
using Microsoft.Xna.Framework.Media; using System.Windows.Media.Imaging;
Step 3: Add two buttons and a image inside ContentPanel grid of MainPage.xaml. The first button will save the image in CameraRoll and second will save the picture in Picture Album.
<Image Height="395" HorizontalAlignment="Left" Margin="6,6,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="444" Source="Rose.png"/>
<Button Content="Save" Height="70" HorizontalAlignment="Center" Margin="6,419,0,0" Name="btnSaveCameraRoll" VerticalAlignment="Top" Width="269" Click="btnSaveCameraRoll_Click" />
<Button Content="Save" Height="70" HorizontalAlignment="Center" Margin="6,480,0,0" Name="btnSave1" VerticalAlignment="Top" Width="269" Click="btnSavePicture_Click" />
Step 4: Create a method StoreFile which will return IsolatedStorageFileSteam. The StoreFile method will be invoked by button click events which we will discuss in next step.
private IsolatedStorageFileStream StoreFile() { string fileName = ((BitmapImage)(image1.Source)).UriSource.OriginalString; using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { if (store.FileExists(fileName)) { store.DeleteFile(fileName); }
IsolatedStorageFileStream storeFile = store.CreateFile(fileName); StreamResourceInfo sri = null; Uri uri = new Uri(fileName, UriKind.Relative); sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage(); bitmap.CreateOptions = BitmapCreateOptions.None; bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap); wb.SaveJpeg(storeFile, wb.PixelWidth, wb.PixelHeight, 0, 100); storeFile.Close();
storeFile = store.OpenFile(fileName, FileMode.Open, FileAccess.Read); return storeFile; } }
Step 5: Add btnSaveCameraRoll_Click method which will save the picture in the CameraRoll.
private void btnSaveCameraRoll_Click(object sender, RoutedEventArgs e) { MediaLibrary library = new MediaLibrary(); IsolatedStorageFileStream storeFile = StoreFile(); Picture pic = library.SavePictureToCameraRoll("SavedPicture.jpg", storeFile); storeFile.Close(); MessageBox.Show("Image saved successfully in camera roll."); }
Don't forget to close the IsolatedStorageFileStream, if you don't close it and you try to do some operation on that exception will be thrown.
Step 6: Add btnSavePicture_Click method which will save the picture in the picture album.
private void btnSavePicture_Click(object sender, RoutedEventArgs e) { MediaLibrary library = new MediaLibrary(); IsolatedStorageFileStream storeFile = StoreFile(); Picture pic = library.SavePicture("SavedPicture.jpg", storeFile); storeFile.Close(); MessageBox.Show("Image saved successfully in picture album"); }
Don't forget to close the IsolatedStorageFileStream, if you don't close it and you try to do some operation on that exception will be thrown.
This ends the article of saving image in Camera Roll or Picture album.
|