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

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

4

3 回答 3

1

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

  1. 从选择器(流)中获取图像
  2. 创建位图对象
  3. 将其写入另一个流
  4. 从第二个流创建一个 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。

于 2012-07-12T13:35:54.907 回答
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 !

于 2013-05-31T05:05:31.157 回答
0

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

在您的尝试块中,只需添加此

var imageVar = new BitmapImage();
imageVar.SetSource(e.ChosenPhoto);
Image img = new Image();
img.Source = imageVar;
this.ContentPanel.Children.Add(img);
于 2012-07-12T12:17:01.310 回答