You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Windows应用中用C#/VB.NET正确解码图片EXIF的XPKeywords重音字符

解决Windows XPKeywords法语重音字符读取乱码问题

问题根源

Windows系统中,XPKeywords属性实际采用**UCS-2LE(小端字节序Unicode)**编码存储,而非笼统的UCS-2。如果读取时误用UTF-8、ASCII等编码,就会导致é、è、à这类重音字符显示为“�”乱码。

C# 解码实现

如果你的代码是读取XPKeywords的原始字节数组,直接用Encoding.Unicode(对应UCS-2LE)解码即可:

// 假设已获取XPKeywords的原始字节数组keywordBytes
byte[] keywordBytes = GetXPKeywordBytes(@"C:\path\to\your\image.jpg");
string correctKeywords = Encoding.Unicode.GetString(keywordBytes);

如果通过Shell API读取属性,确保直接获取正确编码的字符串:

using Shell32; // 需要添加对Shell32.dll的引用

var shell = new Shell();
var folder = shell.NameSpace(@"C:\your\image\folder");
var fileItem = folder.ParseName("test.jpg");
// 18是XPKeywords在Shell属性中的索引(部分系统可能有差异,可循环测试确认)
string keywords = folder.GetDetailsOf(fileItem, 18);

VB 解码实现

VB中同样使用System.Text.Encoding.Unicode处理原始字节:

' 假设已获取XPKeywords的原始字节数组keywordBytes
Dim keywordBytes() As Byte = GetXPKeywordBytes("C:\path\to\your\image.jpg")
Dim correctKeywords As String = System.Text.Encoding.Unicode.GetString(keywordBytes)

通过Shell API读取的示例:

Dim shell As New Shell32.Shell
Dim folder As Shell32.Folder = shell.NameSpace("C:\your\image\folder")
Dim fileItem As Shell32.FolderItem = folder.ParseName("test.jpg")
Dim keywords As String = folder.GetDetailsOf(fileItem, 18)

关键注意事项

  • 禁止用Encoding.UTF8或Encoding.ASCII解码XPKeywords字节流,这是乱码的核心诱因。
  • 若使用第三方图片元数据库(如Exif类库),需检查库的默认编码设置,手动指定UCS-2LE解码该属性。
  • XPKeywords以分号分隔关键词列表,解码后可直接按分号拆分处理。

内容的提问来源于stack exchange,提问作者Phil91

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 20:12:42