Add or Change XMP Metadata to Image
-
Monday, August 20, 2012 11:26 PM
Hi Everyone,
i can't add xmp metadata to jpg image, first i take an jpg image, then do something like crop, resize, convert gray scale
, after that i got a "new jpg image", so i want add xmp metadata (source) to this new jpg image, this is my code:private static void CreateImagenToGrayScale(string filename) { Image img = Image.FromFile(filename); Bitmap bm = new Bitmap(img.Width, img.Height); Graphics g = Graphics.FromImage(bm); //Gilles Khouzams colour corrected grayscale shear ColorMatrix cm = new ColorMatrix(new float[][]{ new float[]{0.3f,0.3f,0.3f,0,0}, new float[]{0.59f,0.59f,0.59f,0,0}, new float[]{0.11f,0.11f,0.11f,0,0}, new float[]{0,0,0,1,0,0}, new float[]{0,0,0,0,1,0}, new float[]{0,0,0,0,0,1}}); ImageAttributes ia = new ImageAttributes(); ia.SetColorMatrix(cm); g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia); g.Dispose(); string filesavename = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + "_GrayScale.jpg"); ImageCodecInfo myImageCodecInfo; System.Drawing.Imaging.Encoder myEncoder; EncoderParameter myEncoderParameter; EncoderParameters myEncoderParameters; myImageCodecInfo = GetEncoderInfo("image/jpeg"); myEncoder = System.Drawing.Imaging.Encoder.Quality; myEncoderParameters = new EncoderParameters(1); myEncoderParameter = new EncoderParameter(myEncoder, 40L); myEncoderParameters.Param[0] = myEncoderParameter; bm.Save(filesavename, myImageCodecInfo, myEncoderParameters); bm.Dispose(); }
private static ImageCodecInfo GetEncoderInfo(string mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for (j = 0; j < encoders.Length; ++j) { if (encoders[j].MimeType == mimeType) return encoders[j]; } return null; }//after that i got "my new jpg image", then i add xmp metadata,
private void AddXMPMetadata(string fileName) { try { InPlaceBitmapMetadataWriter metadataWriter = null; BitmapDecoder decoder = null; string metadaValor = "bonjour"; // Re-opens the file stream as read-write. Stream saveLocationStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); decoder = new JpegBitmapDecoder(saveLocationStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); metadataWriter = decoder.Frames[0].CreateInPlaceBitmapMetadataWriter(); if (metadataWriter.TrySave()) { metadataWriter.SetQuery("/xmp/photoshop:Source", metadaValor); } if (metadataWriter.TrySave()) { metadataWriter.SetQuery("/app13/{ushort=0}/{ulonglong=61857348781060}/{ushort=1}/{str=Source}", metadaValor); } saveLocationStream.Close(); decoder = null; } catch (Exception ex) { throw ex; } }but at the second "metadataWritter.TrySave()" always return false, i dont know what is wrong......
i read this topics, without success
Adding custom XMP metadata - Microsoft Corporation: Software ...
Add XMP metadata to tiff file using BitMapMetadata class
Writing XMPBag Format Metadata to JPEG Images
Add IPTC/XMP-Keywords to a jpeg without touching the image itself ...
Any help will be appreciated.
Lázaro Cruz Software Engineer
All Replies
-
Tuesday, August 21, 2012 12:38 AM
It's not that simple: the in-place metadata writer can only (over)write existing metadata, or add new metadata to existing metadata provided there is some padding space available in the file. In other words the room must exist in the file already for in-place writing to succeed. Since your file does not have any padded XMP metadata, or any metadata for that matter, the in-place writer will always fail.
Thad said your code uses GDI+ (through System.Drawing) and WIC (through System.Windows.Media). You should use only WIC for at least two reasons: System.Drawing is not supported in ASP.NET, the other reason is that GDI+ is deprecated in Windows 8.
Once you use only System.Windows.Media.Imaging you can read and process the image, then add any and all metadata that you need to add, and finally save the whole thing at once. This will be more efficient than the two phases approach that you attempted above.
To add metadata to an existing file, you can try in-place update first then, if that fails, create a new file and transpose your original, i.e. move its components like thumbnail, frame(s), color profile(s) and existing metadata to a new file, adding your new metadata (and padding) along the way, then save the whole thing at once again. The following blog post may help: http://blogs.msdn.com/b/rwlodarc/archive/2007/07/18/using-wpf-s-inplacebitmapmetadatawriter.aspx
- Edited by Axel Rietschin Tuesday, August 21, 2012 12:41 AM
- Edited by Axel Rietschin Tuesday, August 21, 2012 12:42 AM
- Edited by Axel Rietschin Tuesday, August 21, 2012 12:52 AM
- Edited by Axel Rietschin Tuesday, August 21, 2012 12:53 AM
- Edited by Axel Rietschin Tuesday, August 21, 2012 12:54 AM
- Edited by Axel Rietschin Tuesday, August 21, 2012 12:55 AM
- Edited by Axel Rietschin Tuesday, August 21, 2012 1:15 AM
- Marked As Answer by Lázaro Cruz Wednesday, August 22, 2012 8:16 PM
-
Wednesday, August 22, 2012 8:19 PM
Thank you so much. I really appreciate your help, with the post i can add metadata to file image, and yes, change the code, now only use System.Windows.Media.Imaging,
Lázaro Cruz Software Engineer


