Using Chunks in a PNG

拥有回忆 提交于 2019-12-29 07:05:11

问题


I'm looking for a way to get and set those oddball chunks in a PNG file, namely in my case, the 'zTXt' chunk. Is there anything built into the .Net framework, or any other known assembly, that provides access this data? I'd like to avoid having to write an entire PNG reader/writer for this purpose if it has already been done.

Thank you!

Progress update: After more searching, there appears to be nothing pre-made for what I want to accomplish. I'm attempting to read through the file myself now, but am having trouble with the compressed part of the data. I've asked another question for this particular issue here: How do you use a DeflateStream on part of a file?

Once I get this working, I'll post the code here as an answer myself. (Unless someone else beats me to it of course.)


回答1:


The following should work: http://sourceforge.net/projects/pngnet/ Check it out using Subversion. It's a library for low-level access to png files and a small C# example.




回答2:


Does it have to be .net? Here's some ruby code to read through the chunks and write them back out again. Insert your processing in the middle

def extract_chunk(input, output)
   lenword = input.read(4) 
   length = lenword.unpack('N')[0]
   type = input.read(4)
   data = length>0 ? input.read(length) : ""
   crc = input.read(4)
   return nil if length<0 || !(('A'..'z')===type[0,1]) 

   #modify data here.

   output.write [data.length].pack('N')
   output.write type
   output.write data
   output.write crc(data)
   return type
end

def extract_png(input, output)
    hdr = input.read(8)
    raise "Not a PNG File" if hdr[0,4]!= "\211PNG"
    raise "file not in binary mode" if hdr[4,4]!="\r\n\032\n"
    output.write(hdr)
    loop do
      chunk_type = extract_chunk(input,output)
      p chunk_type
      break if  chunk_type.nil? || chunk_type == 'IEND' 
    end
end



回答3:


Here's the code neatly in Java to read an entire PNG. It's pretty small and you should be able to cherry pick what you need.

I would think translating the it c# would be reasonably simple



来源:https://stackoverflow.com/questions/723392/using-chunks-in-a-png

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!