c# - 将图像存储到 Windows Phone 7 中的隔离存储中

标签 c# windows-phone-7

基本上,我使用 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 是显示图像的图像元素。

关于c# - 将图像存储到 Windows Phone 7 中的隔离存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6372422/

相关文章:

c# - WPF RichTextBox 附加彩色文本

css - 使用替代 CSS 解决方案替换 HTML 电子邮件中的 Meta Viewport 标签

windows-phone-7 - C# windows phone -xaml ListBox.ItemTemplate 中的对齐

email - 如何从 Windows Phone 7 发送电子邮件?

windows-phone-7 - 在主机上使用无线适配器时无法将 Windows Phone 模拟器连接到 Internet

c# - 我应该如何在 Windows Phone 7 上使用 RestSharp 实现 ExecuteAsync?

c# - 需要两个单例实例

c# - 从 MP4 视频中提取 JPG 图像

c# - 将数据集转换为 list<double> 和 list<string> C#

c# - Windows.winmd 处于 azure 辅助角色