.net - Edit IPTC Metadata on Jpg vb.net -


i need edit iptc data jpgs.

i used code read keywords has, not able write new ones:

iptc .net read/write c# library

    private sub form2_load(sender object, e eventargs) handles mybase.load     try          dim stream = new filestream(imagepath, filemode.open, fileaccess.read)         dim decoder = new jpegbitmapdecoder(stream, bitmapcreateoptions.none, bitmapcacheoption.none)         dim metadata = trycast(decoder.frames(0).metadata, bitmapmetadata)         if metadata isnot nothing             keywords.text = metadata.keywords.aggregate(function(old, val) convert.tostring(old) & "; " & convert.tostring(val))         end if       catch ex exception         messagebox.show(ex.message)     end try end sub 

any appreciated! thanks

    dim stream = new filestream(imagepath, filemode.open, fileaccess.read)     dim decoder = new jpegbitmapdecoder(stream, bitmapcreateoptions.none, bitmapcacheoption.none)     dim metadata = trycast(decoder.frames(0).metadata, bitmapmetadata)     if metadata isnot nothing         keywords.text = metadata.keywords.aggregate(function(old, val) convert.tostring(old) & "; " & convert.tostring(val))     end if     dim bitmapframe = decoder.frames(0)     metadata = bitmapframe.metadata.clone()     metadata.keywords = new readonlycollection(of string)(new list(of string)(new string() {"test1", "test2"}))     dim stream1 = new filestream(imagepath1, filemode.create)     dim encoder = new jpegbitmapencoder()     encoder.frames.add(bitmapframe.create(bitmapframe, bitmapframe.thumbnail, metadata, bitmapframe.colorcontexts))     encoder.save(stream1) 

update new code replaces image.

imports system.windows.media.imaging imports system.io imports system.collections.objectmodel class mainwindow     private sub window_loaded(sender object, e routedeventargs)         dim imagepath = ".\img.jpg" ' path file         dim stream = new filestream(imagepath, filemode.open, fileaccess.read, fileshare.readwrite)         dim decoder = new jpegbitmapdecoder(stream, bitmapcreateoptions.none, bitmapcacheoption.none)         dim metadata = trycast(decoder.frames(0).metadata, bitmapmetadata)         if metadata isnot nothing             keywords.text = metadata.keywords.aggregate(function(old, val) convert.tostring(old) & "; " & convert.tostring(val))         end if         description.text = metadata.getquery("/app13/irb/8bimiptc/iptc/caption") 'get description         dim bitmapframe = decoder.frames(0)         metadata = bitmapframe.metadata.clone()         dim newkeywords new list(of string)(new string() {"test1", "test2"})         newkeywords.addrange(metadata.keywords) ' strin adds old keywords         metadata.keywords = new readonlycollection(of string)(newkeywords) 'replace keywords         metadata.setquery("/app13/irb/8bimiptc/iptc/caption", "my test picture1.") ' set new description         dim memstream new memorystream() ' create temp storage in memory         dim encoder = new jpegbitmapencoder()         encoder.frames.add(bitmapframe.create(bitmapframe, bitmapframe.thumbnail, metadata, bitmapframe.colorcontexts))         encoder.save(memstream) ' save in memory         stream.close()         stream = new filestream(imagepath, filemode.open, fileaccess.write, fileshare.readwrite)         memstream.seek(0, system.io.seekorigin.begin) ' go stream start         dim bytes(memstream.length) byte         memstream.read(bytes, 0, memstream.length)         stream.write(bytes, 0, bytes.length)         stream.close()         memstream.close()     end sub end class 

jpeg metadata here.
iptc fields here.


Comments