InPlaceBitmapMetadataWriter.TrySave() 返回 true 但不执行任何操作
在 Windows 7 中的某些 .JPG 文件(EPS 预览,由 Adobe Illustrator 生成)上,InPlaceBitmapMetadataWriter.TrySave() 在调用一些 SetQuery() 后返回 true,但不执行任何操作。
代码示例:
BitmapDecoder decoder;
BitmapFrame frame;
BitmapMetadata metadata;
InPlaceBitmapMetadataWriter writer;
decoder = BitmapDecoder.Create(s, BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.Default);
frame = decoder.Frames[0];
metadata = frame.Metadata as BitmapMetadata;
writer = frame.CreateInPlaceBitmapMetadataWriter();
try {
writer.SetQuery("System.Title", title);
writer.SetQuery(@"/app1/ifd/{ushort=" + exiftagids[0] + "} ", (title + '\0').ToCharArray());
writer.SetQuery(@"/app13/irb/8bimiptc/iptc/object name", title);
return writer.TrySave();
}
catch {
return false;
}
您可以通过下载图像示例来重现问题(如果您有 Windows 7)并使用此代码示例在此图像上设置标题。 图像有足够的空间容纳元数据 - 并且此代码示例在我的 WinXP 上运行良好。 相同的代码在 Win7 上与其他 .JPG 文件一起工作正常。
欢迎任何想法:)
On some .JPG files (EPS previews, generated by Adobe Illustrator) in Windows 7 InPlaceBitmapMetadataWriter.TrySave() returns true after some SetQuery() calls, but does nothing.
Code sample:
BitmapDecoder decoder;
BitmapFrame frame;
BitmapMetadata metadata;
InPlaceBitmapMetadataWriter writer;
decoder = BitmapDecoder.Create(s, BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.Default);
frame = decoder.Frames[0];
metadata = frame.Metadata as BitmapMetadata;
writer = frame.CreateInPlaceBitmapMetadataWriter();
try {
writer.SetQuery("System.Title", title);
writer.SetQuery(@"/app1/ifd/{ushort=" + exiftagids[0] + "} ", (title + '\0').ToCharArray());
writer.SetQuery(@"/app13/irb/8bimiptc/iptc/object name", title);
return writer.TrySave();
}
catch {
return false;
}
You can reproduce problem (if you have Windows 7) by downloading image sample and using this code sample to set title on this image.
Image has enough room for metadata - and this code sample works fine on my WinXP.
Same code works fine on Win7 with other .JPG files.
Any ideas are welcome :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两件事:
我认为您无法像那样写入
元数据
变量,因为它将被冻结。因此,您必须克隆它:Padding,您需要填充。我在花了大约一天的时间尝试使一些代码(与您的代码类似)工作后发现了这一点。如果图像文件中没有元数据填充,
InPlaceBitmapMetadataWriter
将无法工作。因此您需要类似以下内容:现在您可以使用位于
_myoutputpath
它为您的InPlaceBitmapMetadataWriter
操作添加了元数据填充。本文和附加代码应该对您有帮助出去。
Two things:
I don't think you will be able to write to your
metadata
variable just like that, as it will be Frozen. So, you will have to clone it:Padding, you need padding. I found this out after about a day's worth of tinkering around trying to make some code (similar to yours) work.
InPlaceBitmapMetadataWriter
will not work if there is no metadata padding in your image file. So you need something like:Now you can use the file located at
_myoutputpath
which has added metadata padding for yourInPlaceBitmapMetadataWriter
operations.This article and attached code should help you out.
嗨,我发现这个 关于 InPlaceBitmapMetadataWriter 的文章,其中有人说 TrySave() 可能会损坏图像,这就是为什么他建议在原始文件的副本上执行 TrySave() ,如果这不起作用,请向副本添加填充原始文件,然后再次 TrySave(),如果有效,则删除原始文件并重命名副本。
我挠头问自己,为什么我应该费心使用 InPlaceBitmapMetadataWriter 并将 3x 原始文件写入磁盘,以防 TrySave() 不起作用,因为没有足够的填充,如果我可以克隆元数据,将任何内容写入其中并组装 jpeg立即归档。
然后我开始认为,也许多亏了 InPlaceBitmapMetadataWriter,我可以在不损失质量的情况下编辑元数据,但看起来它“只是”帮助您在有足够填充的情况下更快地写入元数据。
我写了一个小测试,我多次压缩一个文件来查看质量下降情况,你可以在第三次第四次压缩中看到它,这是非常糟糕的。
但幸运的是,如果您始终对 JpegBitmapEncoder 使用相同的 QualityLevel,则不会出现降级。
在此示例中,我在元数据中重写了 100 倍的关键字,但质量似乎没有改变。
Hi I found this article about InPlaceBitmapMetadataWriter where the guy said that TrySave() might corrupt the image and that's why he advised to do TrySave() on the copy of the original file and if this doesn't work, add padding to the copy of original file and than TrySave() again and if it works, delete the original and rename the copy.
I scratched my head and asked myself why I should bother with InPlaceBitmapMetadataWriter and writing 3x original file to the disk in case TrySave() doesn't work because there is not enough padding, if I can clone metadata, write whatever into them and assemble jpeg file right away.
Then I started to think that maybe thanks to InPlaceBitmapMetadataWriter I can edit metadata without losing quality, but it looks like it "just" helps you to write metadata more quickly if there is enough padding.
I wrote a small test where I compress one file many times to see the quality degradation and you can see it in the third-fourth compression, which is very bad.
But luckily, if you always use same QualityLevel with JpegBitmapEncoder there is no degradation.
In this example I rewrite keywords 100x in metadata and the quality seems not to change.
我仍然没有找到答案,必须为 exiftool 编写包装器,而不是使用 WPF 的方式来处理元数据......
也许 som1 会发现它很有用。
I still didn't find the answer and has to write wrapper for exiftool instead of using WPF's way to work with metadata...
May be som1 will find it useful.