将图像保存到手机存储 [英] Save image to phone storage
本文介绍了将图像保存到手机存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有图像控制,我想将此图像保存到手机存储中.所以,我有图像控件和下面的按钮.当用户单击按钮时,图像应保存到手机存储中.我该怎么做?
I have image control and I want to save this image to phone storage. So, I have Image control and below button. When user click the button, image should be saved to phone storage. How can I do this ?
我找到了代码:
// 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("[Application Name];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();
但是如何将我的图像从图像控件放在这里?
But how I can put my image from image control here ?
推荐答案
从图像控件保存到独立存储的简单示例.
Simple example saving to Isolated Storage from image control.
Stream ImageStream = (MyImageControl.Source as BitmapImage).GetStream();
IsolatedStorageFile IsoStore = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream storageStream = IsoStore.CreateFile("TestImage.bmp"))
ImageStream.CopyTo(storageStream);
public static Stream GetStream(this BitmapImage image)
{
MemoryStream stream = new MemoryStream();
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
return stream;
}
GetStream
扩展来自这个答案 WPF Image to byte[].我还没有测试过这段代码,但它应该能让你走上正轨.
GetStream
extension is taken from this answer WPF Image to byte[]. I haven't tested this code but it should get you on the right track.
这篇关于将图像保存到手机存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文