是ExifLib在WPF / XAML应用程序可以使用吗? [英] Is ExifLib usable in a WPF / XAML app?

查看:232
本文介绍了是ExifLib在WPF / XAML应用程序可以使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从JPG图像中提取EXIF数据。 ExifLib 似乎是一个不错的选择,以简化这个苦差事,所以我经过的NuGet安装它。

我又试图使用示例code从的此处(注释掉在MessageBox code现在):

 使用(ExifReader读卡器=新ExifReader(@C:\ TEMP \ testImage.jpg))
{
    //提取使用ExifTags枚举标签数据
    日期时间datePictureTaken;
    如果(reader.GetTagValue< D​​ateTime的>(ExifTags.DateTimeDigitized,出datePictureTaken))
    {
        //做任何需要与提取的信息
        //System.Windows.MessageBox.Show(this,的String.Format(以下简称这张照片拍摄于{0},
        // datePictureTaken),图像信息); //,MessageBoxButtons.OK);
    }
}
 

但得到一个错误:

  

最好的重载的方法匹配'ExifLib.ExifReader.ExifReader(的System.IO.Stream)'有一些无效的参数

  

参数1:不能从字符串转换为的System.IO.Stream

无论在这一行:

 使用(ExifReader读卡器=新ExifReader(@C:\ TEMP \ testImage.jpg))
 

这是可以解决的,或者是ExifLib从一个WPF / XAML应用程序不能使用?

如果ExifLib是的没有的一个WPF / XAML应用程序,一个可行的解决方案,有什么替代品存在吗?

更新:

通过这个code,从西门麦肯齐的回答是:

 私人无效btnLoadNewPhotoset_Click(对象发件人,RoutedEventArgs E)
{
    使用(VAR店= IsolatedStorageFile.GetUserStoreForApplication())
    使用(VAR流= store.OpenFile(testImage.jpg,FileMode.Open))
    使用(VAR读卡器=新ExifReader(流))
    {
        // ...
    }
}
 

我还得到一个错误:

  

的类型或命名空间名称IsolatedStorage没有命名空间System.IO(是否缺少程序集引用?)存在

这是一个Windows应用商店(C#)在Visual Studio 2013年该项目的属性创建应用表明,它针对的Windows 8.1,并且配置管理器中显示的配置==调试,平台= 64)

我的项目显示的参考文献有:

  .NET为Windows应用商店的应用程序
Bing.Maps.Xaml
ExifLib
微软的Visual C ++运行时包
的Windows 8.1
 

我在想什么?

更新2:

当我看着参考经理Assemblies.Framework,它说,所有的框架组装的已经被引用...我想mscorlib.dll中被认为是其中一个(不一一列举) ?

我搜索我的硬盘mscorlib.dll中和我有一百万人,各种不同大小和日期。哪一个我应该尝试添加作为参考?我用C把一切都从一个:\日期为2012年7月9日以2564528文件大小程序文件(x86)\参考大会\微软\ Framework.NETFramework \ V4.5一个在C:\ Program Files文件(86 )\参考大会\微软\ Framework.NETCore \ V4.5.1来......你的名字。

思考C:\ Program Files文件(x86)的\参考大会\微软\ Framework.NETCore \ V4.5.1似乎是最好的选择,我想通过浏览按钮来引用它,但是当我做到了,我被骂有:

在完全公开的Windows 8.1的利益,在参考经理,它说,在Windows 8.1 SDK中已经提到。

对于Windows 8.1.Extensions,它表明我:

 微软的Visual C ++ 2013运行时包的Windows 12.0(未选中)
微软的Visual C ++ 2013运行时包11.0(选中)
 

由于这似乎是警告之一的原因,我扭转了他们的checkedness(选中2013年, 未经检查的除外)。

我还检查了:

 行为SDK(XAML)12.0
SQLite,让Windows运行时3.8.6(因为我最终将使用SQLite在这个项目中)
 

更新3:

我刚刚发现这一点:。的独立存储不适用于Windows应用商店的应用程序相反,使用应用程序数据类中包含Windows运行时API来存储本地数据和文件的命名空间Windows.Storage 这里

更新4:

我等待着西蒙的例子,但我想这可能是这样的:

 使用Windows.Storage;
使用ExifLib;
。 。 。
私人异步无效btnOpenImgFiles_Click(对象发件人,RoutedEventArgs E)
{
    FileOpenPicker openPicker =新FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.Add(JPG);
    openPicker.FileTypeFilter.Add(JPEG);
    openPicker.FileTypeFilter.Add(。PNG);

    IReadOnlyList< StorageFile>文件=等待openPicker.PickMultipleFilesAsync();
    的for(int i = 0; I< files.Count;我++)
    {
        使用(VAR randomAccessStream =等待文件[I] .OpenAsync(FileAccessMode.Read))
        使用(VAR流= randomAccessStream.AsStream())
        使用(VAR exfrdr =新ExifReader(流))
        {
            // ... exfrdr
        }
    }
}
 

解决方案

string的构造是不是在的Windows Phone / Windows应用商店的应用程序可用,因为他们不能直接文件系统访问。实际上,您将需要通过包含图像流。下面是一个使用 FileOpenPicker 的例子。注意使用 AsStream(...)的 转换的<一个href="http://msdn.microsoft.com/library/windows/apps/windows.storage.streams.irandomaccessstream.aspx"相对=nofollow> IRandomAccessStream 成的 与使用 ExifReader

 使用系统;
使用System.IO;
使用Windows.Storage;
使用Windows.Storage.Pickers;

// ...

VAR选择器=新FileOpenPicker();
picker.FileTypeFilter.Add(JPG);
var文件=等待picker.PickSingleFileAsync();

使用(VAR randomAccessStream =等待file.OpenAsync(FileAccessMode.Read))
{
    使用(VAR流= randomAccessStream.AsStream())
    {
        使用(VAR读卡器=新ExifReader(流))
        {
            弦模型;
            如果(reader.GetTagValue(ExifTags.Model,出模式))
            {
                VAR对话框=新MessageDialog(模式,相机型号);
                dialog.ShowAsync();
            }
        }
    }
}
 

I want to extract exif data from jpg images. ExifLib seemed like a good choice to simplify this chore, and so I installed it via NuGet.

I then tried to get started using the sample code from here (commenting out the MessageBox code for now):

using (ExifReader reader = new ExifReader(@"C:\temp\testImage.jpg"))
{
    // Extract the tag data using the ExifTags enumeration
    DateTime datePictureTaken;
    if (reader.GetTagValue<DateTime>(ExifTags.DateTimeDigitized, out datePictureTaken))
    {
        // Do whatever is required with the extracted information
        //System.Windows.MessageBox.Show(this, string.Format("The picture was taken on {0}",
        //   datePictureTaken), "Image information"); //, MessageBoxButtons.OK);
    }
}

but get an error:

The best overloaded method match for 'ExifLib.ExifReader.ExifReader(System.IO.Stream)' has some invalid arguments

and

Argument 1: cannot convert from 'string' to 'System.IO.Stream'

both on this line:

using (ExifReader reader = new ExifReader(@"C:\temp\testImage.jpg"))

Is this fixable, or is ExifLib not usable from a WPF / XAML app?

If ExifLib is not a viable solution for a WPF / XAML app, what alternatives exist?

Update:

With this code, from Simon McKenzie's answer:

private void btnLoadNewPhotoset_Click(object sender, RoutedEventArgs e)
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    using (var stream = store.OpenFile("testImage.jpg", FileMode.Open))
    using (var reader = new ExifReader(stream))
    {
        // ...
    }
}

I still get an error:

The type or namespace name 'IsolatedStorage' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)

This is a Windows Store (C#) app created in Visual Studio 2013. The project's properties shows that it targets Windows 8.1, and Configuration Manager shows configuration == debug, platform = x64)

My project's displayed References are:

.NET for Windows Store apps
Bing.Maps.Xaml
ExifLib
Microsoft Visual C++ Runtime Package
Windows 8.1

What am I missing?

Update 2:

When I look in Reference Manager at Assemblies.Framework, it says, "All of the Framework assembles are already referenced..." I assume mscorlib.dll is supposed to be one of these (it doesn't list them)?

I searched my hard drive for "mscorlib.dll" and I've got a million of them, all different sizes and dates. Which one should I try to add as a reference? I've got everything from one in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5 dated 7/9/2012 with file size of 2,564,528 to one in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5.1 to...you name it.

Thinking "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5.1" seemed the best bet, I tried to reference it via the Browse button, but when I did, I got scolded with:

In the interests of full disclosure, in Reference Manager for Windows 8.1, it says, "The Windows 8.1. SDK is already referenced."

For Windows 8.1.Extensions, it shows me:

Microsoft Visual C++ 2013 Runtime Package for Windows 12.0 (unchecked)
Microsoft Visual C++ 2013 Runtime Package 11.0 (checked)

Since this seems to be the cause of one of the warnings, I reversed their checkedness (checked 2013, unchecked the other).

I also checked:

Behaviors SDK (XAML) 12.0
SQLite for Windows Runtime 3.8.6 (because I will eventually be using SQLite in this project)

Update 3:

I just found this: "Isolated storage is not available for Windows Store apps. Instead, use the application data classes in the Windows.Storage namespaces included in the Windows Runtime API to store local data and files." here.

Update 4:

I'm waiting for Simon's example, but I'm thinking it might be something like this:

using Windows.Storage;
using ExifLib;
. . .
private async void btnOpenImgFiles_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.FileTypeFilter.Add(".jpeg");
    openPicker.FileTypeFilter.Add(".png");

    IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();
    for (int i = 0; i < files.Count; i++)
    {
        using (var randomAccessStream = await files[i].OpenAsync(FileAccessMode.Read))
        using (var stream = randomAccessStream.AsStream())
        using (var exfrdr = new ExifReader(stream))
        {
            // ...exfrdr
        }
    }
}

解决方案

The string constructor is not available in Windows Phone/Windows Store apps because they aren't allowed direct filesystem access. You will instead need to pass in a stream containing your image. Here's an example using a FileOpenPicker. Note the use of AsStream(...) to convert the IRandomAccessStream into a Stream for use with an ExifReader.

using System;
using System.IO;
using Windows.Storage;
using Windows.Storage.Pickers;

// ...

var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".jpg");
var file = await picker.PickSingleFileAsync();

using (var randomAccessStream = await file.OpenAsync(FileAccessMode.Read))
{
    using (var stream = randomAccessStream.AsStream())
    {
        using (var reader = new ExifReader(stream))
        {
            string model;
            if (reader.GetTagValue(ExifTags.Model, out model))
            {
                var dialog = new MessageDialog(model, "Camera Model");
                dialog.ShowAsync();
            }
        }
    }
}

这篇关于是ExifLib在WPF / XAML应用程序可以使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆