将图像存储到windowsphone 7中的独立存储器中
本文关键字:独立 存储器 windowsphone 图像 存储 | 更新日期: 2023-09-27 17:59:13
基本上,我使用Visual Studio/Expression Blend来完成我的应用程序。它的工作原理是,用户可以选择他/她想要编辑的图片,编辑后,用户只需单击保存按钮,编辑后的图像就会保存在隔离存储中,但我无法命令保存按钮将图像保存到隔离存储中。所以希望有人能帮我提供一些示例代码,非常感谢。
我尝试了下面的代码,但当我按下保存按钮时,出现了一个空引用错误。我的想法是,当你按下保存时,应用程序不知道该将哪张图像保存到隔离存储中,也不确定我的想法是否正确。有人能帮我吗。非常感谢。
private void btnSave_Click(object sender, RoutedEventArgs e)
{
String tempJPEG = "TempJPEG";
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(tempJPEG))
{
myStore.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);
Uri uri = new Uri("TestImage.jpg", UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
Extensions.SaveJpeg(wb, myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
myFileStream.Close();
这是代码的工作版本
private void saveButtonClick(object sender, RoutedEventArgs e)
{
try
{
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isf.FileExists("myImage.jpg"))
isf.DeleteFile("myImage.jpg");
using (var isfs = isf.CreateFile("myImage.jpg"))
{
var bmp = new WriteableBitmap(myImageElement,
myImageElement.RenderTransform);
bmp.SaveJpeg(isfs, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
这里myImageElement
是用于显示图像的图像元素。