【问题标题】:ExifLib requires a seekable streamExifLib 需要一个可搜索的流
【发布时间】:2014-03-14 12:12:43
【问题描述】:

在 Windows Phone 8 C# 中实例化 ExifReader 时出现错误。请在下面找到代码 sn-p。请做需要的人

错误:“ExifLib 需要可搜索的流”

byte[] imageBytes = (byte[])PhoneApplicationService.Current.State["ViewImage"];

MemoryStream ms = new MemoryStream(imageBytes,0,imageBytes.Length);

BitmapImage bitmapImage = new BitmapImage();

bitmapImage.SetSource(ms);

try
{
    ExifReader xif = new ExifReader(toStream(bitmapImage)); // Getting Error here
    double gpsLat, gpsLng;
    xif.GetTagValue<double>(ExifTags.GPSLatitude, out gpsLat);
    xif.GetTagValue<double>(ExifTags.GPSLongitude, out gpsLng);

    map.Center = new System.Device.Location.GeoCoordinate(gpsLat, gpsLng);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message.ToString());
}

Stream toStream(BitmapImage img)
{
    WriteableBitmap bmp = new WriteableBitmap((BitmapSource)img);
    using (MemoryStream stream = new MemoryStream())
    {
        bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
        stream.Position = 0;
        return stream;
    }
}

【问题讨论】:

    标签: c# windows-phone-8 exif bitmapimage exiflib


    【解决方案1】:

    CanSeek 被调用时,您的MemoryStream 返回false。这是因为您已将 MemoryStream 包装在 using 语句中,这意味着您正在返回一个已处置的对象。

    您的 toStream 方法实际上应该如下所示:

    Stream ToStream(BitmapImage img)
    {
        MemoryStream stream = new MemoryStream();
        using (WriteableBitmap bmp = new WriteableBitmap((BitmapSource)img))
        {
            bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
        }
        stream.Position = 0;
        return stream;
    }
    

    【讨论】:

    • 不用担心。再想一想,您实际上应该将bmp 放入using 语句中,但是,因为所有GDI+ 对象都使用非托管资源。我已编辑我的答案以显示此更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    相关资源
    最近更新 更多