访问isolotedstorage WP7时发生错误

时间:2012-07-12 11:44:55

标签: windows-phone-7 windows-phone-7.1 windows-phone isolatedstorage

我正在尝试从Picture Hub加载图片......

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;
        }
    }

访问isolotedstorage时发生错误。请帮忙!

3 个答案:

答案 0 :(得分:1)

如果我理解正确,你会尝试:

  1. 从选择器(流)获取图像
  2. 创建位图对象
  3. 将其写入另一个流
  4. 从第二个流
  5. 创建一个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方法创建完整大小的WriteableBitmap,则需要执行以下操作:

    //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。

答案 1 :(得分:0)

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

在您的try块中,只需添加此

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

答案 2 :(得分:0)

您使用的代码看起来没问题!

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;
    }
}

尝试重新连接USB!