如何向 PNG 文件添加属性

发布于 2024-09-15 23:59:07 字数 461 浏览 6 评论 0原文

我有一个 PNG 文件,我想向其中添加属性

  1. “每单位像素”、“X 轴
  2. 每单位像素”、“Y 轴”
  3. 单位说明符:米

这些属性在 PNG 规范中进行了解释: http://www.w3.org/TR/PNG-Chunks.html

我已以编程方式读取 的属性.png 检查属性是否存在,以便我可以设置此属性的值,但我在 .png 文件中看不到此属性。 (参考pixel-per-unit.JPG)

我们如何向.png文件添加属性?

问候 替代文本

I have a PNG file to which I want to add the properties

  1. Pixels per unit, X axis
  2. Pixels per unit, Y axis
  3. Unit specifier: meters

These properties are explained in the PNG specification: http://www.w3.org/TR/PNG-Chunks.html

I have programmatically read the properties of the .png to check if the properties exists, so that I can set the value for this properties, but I could not see this properties in the .png file.
(Refer pixel-per-unit.JPG)

How can we add properties to the .png file?

regards
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌生 2024-09-22 23:59:07

尝试使用 pngcs 库(您需要将下载的 dll 重命名为“pngcs.dll”)

我需要添加一些自定义文本属性,但您可以轻松执行更多操作。

这是我添加自定义文本属性的实现:

using Hjg.Pngcs;  // https://code.google.com/p/pngcs/
using Hjg.Pngcs.Chunks;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkerGenerator.Utils
{
    class PngUtils
    {

        public string getMetadata(string file, string key)
        {

            PngReader pngr = FileHelper.CreatePngReader(file);
            //pngr.MaxTotalBytesRead = 1024 * 1024 * 1024L * 3; // 3Gb!
            //pngr.ReadSkippingAllRows();
            string data = pngr.GetMetadata().GetTxtForKey(key);
            pngr.End();
            return data; ;
        }


        public static void addMetadata(String origFilename, Dictionary<string, string> data)
        {
            String destFilename = "tmp.png";
            PngReader pngr = FileHelper.CreatePngReader(origFilename); // or you can use the constructor
            PngWriter pngw = FileHelper.CreatePngWriter(destFilename, pngr.ImgInfo, true); // idem
            //Console.WriteLine(pngr.ToString()); // just information
            int chunkBehav = ChunkCopyBehaviour.COPY_ALL_SAFE; // tell to copy all 'safe' chunks
            pngw.CopyChunksFirst(pngr, chunkBehav);          // copy some metadata from reader 
            foreach (string key in data.Keys)
            {
                PngChunk chunk = pngw.GetMetadata().SetText(key, data[key]);
                chunk.Priority = true;
            }

            int channels = pngr.ImgInfo.Channels;
            if (channels < 3)
                throw new Exception("This example works only with RGB/RGBA images");
            for (int row = 0; row < pngr.ImgInfo.Rows; row++)
            {
                ImageLine l1 = pngr.ReadRowInt(row); // format: RGBRGB... or RGBARGBA...
                pngw.WriteRow(l1, row);
            }
            pngw.CopyChunksLast(pngr, chunkBehav); // metadata after the image pixels? can happen
            pngw.End(); // dont forget this
            pngr.End();
            File.Delete(origFilename);
            File.Move(destFilename, origFilename);

        }

        public static void addMetadata(String origFilename,string key,string value)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add(key, value);
            addMetadata(origFilename, data);
        }


    }
}

Try using pngcs library (you need to rename the downloaded dll to "pngcs.dll")

I needed to add some custom text properties, but you can easily do much more.

Here is my implementation for adding custom text properties:

using Hjg.Pngcs;  // https://code.google.com/p/pngcs/
using Hjg.Pngcs.Chunks;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkerGenerator.Utils
{
    class PngUtils
    {

        public string getMetadata(string file, string key)
        {

            PngReader pngr = FileHelper.CreatePngReader(file);
            //pngr.MaxTotalBytesRead = 1024 * 1024 * 1024L * 3; // 3Gb!
            //pngr.ReadSkippingAllRows();
            string data = pngr.GetMetadata().GetTxtForKey(key);
            pngr.End();
            return data; ;
        }


        public static void addMetadata(String origFilename, Dictionary<string, string> data)
        {
            String destFilename = "tmp.png";
            PngReader pngr = FileHelper.CreatePngReader(origFilename); // or you can use the constructor
            PngWriter pngw = FileHelper.CreatePngWriter(destFilename, pngr.ImgInfo, true); // idem
            //Console.WriteLine(pngr.ToString()); // just information
            int chunkBehav = ChunkCopyBehaviour.COPY_ALL_SAFE; // tell to copy all 'safe' chunks
            pngw.CopyChunksFirst(pngr, chunkBehav);          // copy some metadata from reader 
            foreach (string key in data.Keys)
            {
                PngChunk chunk = pngw.GetMetadata().SetText(key, data[key]);
                chunk.Priority = true;
            }

            int channels = pngr.ImgInfo.Channels;
            if (channels < 3)
                throw new Exception("This example works only with RGB/RGBA images");
            for (int row = 0; row < pngr.ImgInfo.Rows; row++)
            {
                ImageLine l1 = pngr.ReadRowInt(row); // format: RGBRGB... or RGBARGBA...
                pngw.WriteRow(l1, row);
            }
            pngw.CopyChunksLast(pngr, chunkBehav); // metadata after the image pixels? can happen
            pngw.End(); // dont forget this
            pngr.End();
            File.Delete(origFilename);
            File.Move(destFilename, origFilename);

        }

        public static void addMetadata(String origFilename,string key,string value)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add(key, value);
            addMetadata(origFilename, data);
        }


    }
}
中二柚 2024-09-22 23:59:07

我认为您正在寻找 SetPropertyItem。您可以在此处

您可以使用属性 id 来获取并设置元数据的属性项。

编辑

您需要的三个ID(我认为)是:

  1. 0x5111 - 每单位像素 X
  2. 0x5112 - 每单位 Y 像素
  3. 0x5110 - 像素单位

I think you are looking for SetPropertyItem. You can find the property ids here

You would use the property id to get and then set the property item for your meta-data.

EDIT

The three id's that you need (I think) are:

  1. 0x5111 - Pixel Per Unit X
  2. 0x5112 - Pixel Per Unit Y
  3. 0x5110 - Pixel Unit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文