Windows phone 7 访问IsolatedStorage WP7时出错

Windows phone 7 访问IsolatedStorage WP7时出错,windows-phone-7,windows-phone-7.1,windows-phone,isolatedstorage,Windows Phone 7,Windows Phone 7.1,Windows Phone,Isolatedstorage,我正在尝试通过这个从图片中心加载图像 void photoChooser_Completed(object sender, PhotoResult e) { try { var imageVar = new BitmapImage(); imageVar.SetSource(e.ChosenPhoto); var b = new Writea

我正在尝试通过这个从图片中心加载图像

void photoChooser_Completed(object sender, PhotoResult e)
    {           
        try
        {
            var imageVar = new BitmapImage();
            imageVar.SetSource(e.ChosenPhoto);           
            var b = new WriteableBitmap(imageVar.PixelWidth, imageVar.PixelHeight);
            b.LoadJpeg(toStream(imageVar));//here comes the exception
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    }

Stream toStream(BitmapImage img) 
    {
        WriteableBitmap bmp = new WriteableBitmap((BitmapSource)img);

        using (MemoryStream stream = new MemoryStream())
        {

            bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
            return stream;
        }
    }

访问隔离存储时发生错误。请帮忙

您没有指定获取图像后要执行的操作。 如果您只想在应用程序中显示图像,请遵循以下代码:

在try块中,只需添加以下内容

var imageVar = new BitmapImage();
imageVar.SetSource(e.ChosenPhoto);
Image img = new Image();
img.Source = imageVar;
this.ContentPanel.Children.Add(img);

如果我理解正确,您正在尝试:

  • 从选择器获取图像(流)
  • 创建位图对象
  • 将其写入另一个流
  • 从第二个流创建一个WriteableBitmap
  • 这是非常复杂的。你所要做的就是:

    var imageVar = new BitmapImage();
    imageVar.SetSource(e.ChosenPhoto);           
    var b = new WriteableBitmap(imageVar.PixelWidth, imageVar.PixelHeight);
    b.SetSource(e.ChosenPhoto);
    
    这将获得照片,但请记住,如果首先使用SetSource方法创建BitmapImage,则会将照片大小限制在2000x2000以下。然后,WriteableBitmap也将具有更小、更小的大小

    如果希望使用LoadJpeg方法创建全尺寸可写位图,则需要执行以下操作:

    //DO SOMETHING TO GET THE PIXEL WIDTH AND PIXEL HEIGHT OF PICTURE BASED JUST ON THE STREAM, FOR EXAMPLE USE EXIF READER: http://igrali.com/2011/11/01/reading-and-displaying-exif-photo-data-on-windows-phone/ OR SEE MORE ABOUT LOADING A LARGE PHOTO HERE: http://igrali.com/2012/01/03/how-to-open-and-work-with-large-photos-on-windows-phone/          
    var b = new WriteableBitmap(PixelWidth, PixelHeight);
    b.LoadJpeg(e.ChosenPhoto);
    

    这将加载完整大小的JPEG。

    您使用的代码看起来不错

    void photoChooser_Completed(object sender, PhotoResult e)
    {           
        try
        {
            var imageVar = new BitmapImage();
            imageVar.SetSource(e.ChosenPhoto);           
            var b = new WriteableBitmap(imageVar.PixelWidth, imageVar.PixelHeight);
            b.LoadJpeg(toStream(imageVar));//here comes the exception
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    
    
    }
    
    流到流(位图图像img) { WriteableBitmap bmp=新的WriteableBitmap((位图源)img)

    尝试重新连接USB

        using (MemoryStream stream = new MemoryStream())
        {
    
            bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
            return stream;
        }
    }