本文介绍了将图像存储到 Windows Phone 7 中的独立存储中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我使用 Visual Studio/Expression Blend 来做我的应用程序.它的工作原理是用户可以选择他/她想要编辑的图片,编辑后用户只需单击保存按钮,编辑后的图像将保存在独立存储中,但我无法命令保存按钮进行保存图像进入隔离存储,所以希望有人能帮助我提供一些示例代码,非常感谢.

Basically I am using Visual Studio/Expression Blend to do my app. Its works as in the user can select the picture that he/she wants to edit and after editing the user just had to click the save button and the edited image will be save in isolated storage but I just could not command the save button to save the image into the isolated storage so hope someone will help me with it with some sample codes thanks a lot in advance.

我尝试了下面的代码,但是当我按下保存按钮时,出现了一个空引用错误.我的想法是,当您按保存时,应用程序不知道将哪个图像保存到隔离存储中,并且不确定我的想法是否正确.任何人都可以帮我解决这个问题.非常感谢.

I tried with the code below but when I press the save button there's a null reference error. My thinking is that when you press save, the app does not know which image to save into the isolated storage and not really sure my thinking is right. Can anyone please help me with this. Thanks a lot.

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 是显示图像的图像元素.

Here myImageElement is the Image Element in which you display the image.

这篇关于将图像存储到 Windows Phone 7 中的独立存储中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:09