Reputation: 11
I am developing an application which has many images & i want to implement that on clicking "save button" the Image should be stored to Photo Album.
Please reply i am new to app dev.
Upvotes: 1
Views: 449
Reputation: 183
I found on something like that:source https://msdn.microsoft.com.
// Informs when full resolution photo has been taken, saves to local media library and the local folder.
void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
{
string fileName = savedCounter + ".jpg";
try
{ // Write message to the UI thread.
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Captured image available, saving photo.";
});
// Save photo to the media library camera roll.
library.SavePictureToCameraRoll(fileName, e.ImageStream);
// Write message to the UI thread.
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Photo has been saved to camera roll.";
});
// Set the position of the stream back to start
e.ImageStream.Seek(0, SeekOrigin.Begin);
// Save photo as JPEG to the local folder.
using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
{
// Initialize the buffer for 4KB disk pages.
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
// Copy the image to the local folder.
while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
targetStream.Write(readBuffer, 0, bytesRead);
}
}
}
// Write message to the UI thread.
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Photo has been saved to the local folder.";
});
}
finally
{
// Close image stream
e.ImageStream.Close();
}
}
// Informs when thumbnail photo has been taken, saves to the local folder
// User will select this image in the Photos Hub to bring up the full-resolution.
public void cam_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e)
{
string fileName = savedCounter + "_th.jpg";
try
{
// Write message to UI thread.
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Captured image available, saving thumbnail.";
});
// Save thumbnail as JPEG to the local folder.
using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
{
// Initialize the buffer for 4KB disk pages.
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
// Copy the thumbnail to the local folder.
while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
targetStream.Write(readBuffer, 0, bytesRead);
}
}
}
// Write message to UI thread.
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Thumbnail has been saved to the local folder.";
});
}
finally
{
// Close image stream
e.ImageStream.Close();
}
}
Upvotes: 0
Reputation: 1092
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;
}
Heres more to it http://www.codeproject.com/Articles/747273/How-to-Save-Image-in-Local-Photos-album-of-Windows
Upvotes: 1
Reputation: 638
Try this code. This is in VB.Net but I am sure you can convert it yourself or through online tools for C#:
' Create a file name for the JPEG file in isolated storage.
Dim tempJPEG As String = "dummyImage1"
' Create a virtual store and file stream. Check for duplicate tempJPEG files.
Dim myStore = IsolatedStorageFile.GetUserStoreForApplication()
If myStore.FileExists(tempJPEG) Then
myStore.DeleteFile(tempJPEG)
End If
Dim myFileStream As IsolatedStorageFileStream = myStore.CreateFile(tempJPEG)
' Create a stream out of the sample JPEG file.
' For [Application Name] in the URI, use the project name that you entered
' in the previous steps. Also, TestImage.jpg is an example;
' you must enter your JPEG file name if it is different.
Dim sri As StreamResourceInfo = Nothing
Dim uri As New Uri("/projectName;component/Assets/1.jpg", UriKind.Relative)
sri = Application.GetResourceStream(uri)
' Create a new WriteableBitmap object and set it to the JPEG stream.
Dim bitmap As New BitmapImage()
bitmap.CreateOptions = BitmapCreateOptions.None
bitmap.SetSource(sri.Stream)
Dim wb As New WriteableBitmap(bitmap)
' Encode WriteableBitmap object to a JPEG stream.
wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85)
myFileStream.Close()
' Create a new stream from isolated storage, and save the JPEG file to the media library on Windows Phone.
myFileStream = myStore.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read)
' Save the image to the camera roll or saved pictures album.
Dim library As New MediaLibrary()
' Save the image to the saved pictures album.
Dim pic As Picture = library.SavePicture("dummyImage1.jpg", myFileStream)
MessageBox.Show("Image saved to saved pictures album")
myFileStream.Close()
Upvotes: 0