tags:

views:

294

answers:

3

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

A: 

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
AShelly
That's pretty darn neat, but unfortunately this is going to be a WPF application, so it it does need to be accessible by a .Net language. This makes me wonder if that 'IronRuby' thingamajig could compile it into a .Net assembly, but I know less than nothing about Ruby. =[
YotaXP
There is a wrapper for Ruby: http://www.igvita.com/2007/04/23/invoking-ruby-in-c-net/Maybe...
boj
A: 

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

ShuggyCoUk
+2  A: 

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.

Peter Nowotnick