将元数据写入 .NET 中的 PNG 图像
我看到很多问题询问如何从图像中读取元数据,但我还没有看到那么多问题询问如何编写元数据。基本上,我需要将一项元数据(“ImageDescription”)添加到我动态生成的 PNG 图像中(创建一个 Bitmap 对象并按程序生成其中的所有内容)。
在将文件写入磁盘之前或之后,使用 .NET 将元数据添加到图像的最佳方法是什么?
I see more than a few questions asking how to read metadata from an image, but I haven't seen as many asking how to write metadata. Basically, I need to add one item of metadata ("ImageDescription") to a PNG image I'm generating dynamically (creating a Bitmap object and procedurally generating all the content in it).
What would be the best way to add metadata to an image with .NET either before or just after writing the file to disk?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 FreeImage.NET 库,我相信它可以读取和写入 PNG 文件,以及他们的元数据。
You could use the FreeImage.NET library, which I believe can both read and write PNG files, as well as their metadata.
您可以使用 System.Windows.Media.Imaging 中的 BitmapMetadata 写入 iTXt 值。
VB 示例:
You can use BitmapMetadata from System.Windows.Media.Imaging to write iTXt values.
VB Example:
需要将一些简单的元数据至少写入 .png 和 .jpg 中,而不必将另一个第 3 方库拖到我的程序中。几个小时后似乎没有任何效果,即使所有文件类型都不同等等。
考虑只是存储在图像旁边的文件中或保留一个小型文件“数据库”和我需要的数据。
对我来说,我只是在重新加载图像时再次需要数据。数据包含一个转换系数,告诉我有多少像素 = 1 毫米。
剧透...如果您不喜欢黑客行为,请立即离开。
基本上在文件的末尾,我用二进制“@Conv=”编写,后跟该值的 8 字节双精度值。
我的程序以二进制读取方式打开图像...查找末尾减去我的“hack”中的总字节数,并查找“@Conv=”。如果找到,则读取该值。然后,程序可以选择在确定比例因子后进行保存,而不必每次打开时都进行测量和输入值。
我有代码来读取/查询、更新和添加数据。
适用于 .png、.jpg 以及可能的其他类型。
我执行此操作的文件是复制的并且是本地的。显然,如果从其他应用程序重写文件,数据就会丢失,但对于许多应用程序的任何元数据来说可能都是如此。
我最终存储了一些数据项,包括图像的“原点”位置(以像素为单位)和首选旋转(0,90,180,270)。
一些vbcode只是为了好玩。
Needed to write some simple metadata into at least .png and .jpg and not have to drag another 3rd party library into my program. Nothing seemed to work after a few hours and it seems even if it was all the file type are different etc.
Thought about just storing in a file next to the image or keeping a small "database" of files and the data I needed.
For me, I just needed the data again when I reloaded the images. The data held a conversion factor that tells me how many pixels = 1mm.
Spoiler... turn away now if you don't like hacks.
Basically at the end of the file I wrote in binary "@Conv=" followed by the 8 byte double of the value.
My program opens the image in a binary read... seeks to the end minus then number of bytes total in my "hack" and looks for "@Conv=". If this is found it reads the value(s). The program then has the options to save once the scale factor is determined rather than having to measure and enter a value every time when opening.
I have code to read/query, update and add the data.
Works well with .png, .jpg and probably other types.
The files I do it to are copied and are local. Obviously if the files are re-written from some other application the data would be lost but that's probably true of any metadata for a lot of applications.
I ended up storing a few data items including an "origin" position (in pixels) and the preferred rotation (0,90,180,270) of the image.
Some vbcode just for kicks.