如何添加“评论”使用 C# 转换为 JPEG 文件
在 JPEG 图像的属性窗口中,有一个名为“摘要”的选项卡。在此选项卡中,有一个名为“评论”的字段,我想编写一些 C# 代码,将给定的字符串添加到该字段,例如“这是一张照片”。
有好心人知道怎么做吗?
非常感谢。
Within the property window of a JPEG image, there is a tab called 'Summary'. Within this tab, there is a field called 'Comments' I would like to write some c# code which will add a given string to this field e.g "This is a photo".
Does some kind soul out there know how to do this?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据其他答案,我编写了以下允许各种元数据操作的类。你可以这样使用它:
我的解决方案和其他解决方案之间的差异并不大。主要是我对其进行了重构以使其更加清晰。我还使用
BitmapMetadata
的更高级别属性,而不是SetQuery
方法。以下是完整代码,已根据MIT 许可证获得许可。您需要添加对
PresentationCore
、WindowsBase
和System.Xaml
的引用。Based on other answers I wrote the following class which allows various metadata manipulations. You use it like this:
The differences between my solution and the others are not large. Principally I have refactored this to be cleaner. I also use the higher level properties of
BitmapMetadata
, rather than theSetQuery
method.Here is the full code, which is licensed under the MIT licence. You will need to add references to
PresentationCore
,WindowsBase
, andSystem.Xaml
.下面的代码解决了我的问题,并向给定的 JPEG 图像添加了注释:
这本质上是对 Konamiman 善意提供的链接下的代码进行了轻微修改的版本。
请注意,要实现此功能,您需要添加对 PresentationCore 和 WindowsBase 的 .NET 引用。如果使用 Visual Studio 2008,可以通过以下方式实现:
在解决方案资源管理器中右键单击您的项目
从下拉列表中选择“添加” “参考...”
从打开的新框中选择“.NET”选项卡
滚动到上面提到的两个参考,在每个参考上单击“确定”
非常感谢 danbystrom 和 Konamiman你在这件事上的帮助。我真的很感谢您的快速回复。
The following code solves my problem and adds comments to a given JPEG image:
This is essentially a lightly modified version of the code found under the link which Konamiman kindly supplied.
Please be aware that to make this work you will need to add .NET references to PresentationCore and WindowsBase. If using Visual Studio 2008, this can be achieved via the following:
Right click on your project in the Solution Explorer
From the drop down list, select Add 'Reference...'
From the new box which opens, select the '.NET' tab
Scroll to the two references mentioned above and on each, click ok
Many thanks to both danbystrom and Konamiman for your help in this matter. I really appreciate the quick response.
简单的部分:
添加此属性项:
到图像的 PropertItems 集合。
比较麻烦的部分:
由于没有公共构造函数,如何创建新的 PropertyItem?
常见的“技巧”是放置一个空图像,您可以从中窃取 PropertyItem。 叹息
The easy part:
Add this property item:
To the Image's PropertItems collection.
The somewhat more cumbersome part:
How do you create a new PropertyItem, since it has no public constructor?
The common "trick" is to have an empty image lying around from which you can steal a PropertyItem. sigh
感谢这里的答案,我编写了一个仅使用内存设置注释的解决方案:
不要忘记处理此方法返回的图像。 (例如将图像保存到文件后)
Thanks to the answers here, I've coded a solution to set a comment using memory only:
Don't forget to dispose the image that is returned by this method. (For example after saving the image to a file)
Peter Kistler 设置标题、主题和评论解决方案的变体。我发现我必须将项目创建为 Unicode 字节数组(类型 1),并且标题、主题和评论的 ID 与 EXIF XPTitle、XPSubject 和 XP Comment 的 ID 相同。 sFileOut 可以与 sFile 相同。
A variant on Peter Kistler's solution to set Title, Subject and Comment. I found I had to create items as Unicode byte array (type 1) and the IDs for Title, Subject and Comment are the same as for EXIF XPTitle, XPSubject and XP Comment. sFileOut can be the same as sFile.
感谢之前的提示,我能够将以下内容放在一起。我已经测试过了,它似乎有效。最大的障碍之一是确定要分配的字段所需的 ID。
Thanks to the previous tips I was able to put the following together. I've tested it and it seems to work. One of the biggest stumbling blocks was determining the Id needed for the field you want to assign.