YotaXP
YotaXP

Reputation: 4064

Using Chunks in a PNG

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.)

Upvotes: 5

Views: 4626

Answers (3)

Peter Nowotnick
Peter Nowotnick

Reputation: 151

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.

Upvotes: 4

ShuggyCoUk
ShuggyCoUk

Reputation: 36438

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

Upvotes: 0

AShelly
AShelly

Reputation: 35540

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

Upvotes: 1

Related Questions