保存图片到手机图片库
最近因为项目需要,需将图片保存至手机图片库,以前都只是将图片存放在独立存储中,刚好学习了一下~
前台只是一个简单的图片控件,保存按钮,然后再一按钮用于查看我们是否将图片保存到手机图片库中~
<Image Height="395"
HorizontalAlignment="Left"
Margin="6,6,0,0"
Name="image1"
Stretch="Fill"
VerticalAlignment="Top"
Width="444"
Source="/SavePicture;component/TestImage.jpg"/>
<Button Content="Save"
Height="70"
HorizontalAlignment="Center"
Margin="-12,516,319,0"
Name="btnSave"
VerticalAlignment="Top"
Width="149" Click="btnSave_Click" />
<Button Content="choosepic" Height="72" HorizontalAlignment="Left" Margin="206,516,0,0" Name="button1" VerticalAlignment="Top" Width="177" Click="button1_Click" />
后台
private void btnSave_Click(object sender, RoutedEventArgs e)
{
// Create a file name for the JPEG file in isolated storage.
String tempJPEG = "TempJPEG";
// Create a virtual store and file stream. Check for duplicate tempJPEG files.
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(tempJPEG))
{
myStore.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream myFileStream = 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.
StreamResourceInfo sri = null;
Uri uri = new Uri("SavePicture;component/TestImage.jpg", UriKind.Relative);
sri = Application.GetResourceStream(uri);
// Create a new WriteableBitmap object and set it to the JPEG stream.
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode the 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.
MediaLibrary library = new MediaLibrary();
Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
MessageBox.Show("Image saved to saved pictures album");
myFileStream.Close();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask task = new PhotoChooserTask();
task.Completed += new EventHandler<PhotoResult>(task_Completed);
task.Show();
}
void task_Completed(object sender, PhotoResult e)
{
}


主要实现是将图片流保存到多媒体库中
Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
浙公网安备 33010602011771号