How to Add 'Comments' to a Jpeg File Using C#

How to Add 'Comments' to a JPEG File Using C#

The following code solves my problem and adds comments to a given JPEG image:

public void addImageComment(string imageFlePath, string comments)
{
string jpegDirectory = Path.GetDirectoryName(imageFlePath);
string jpegFileName = Path.GetFileNameWithoutExtension(imageFlePath);

BitmapDecoder decoder = null;
BitmapFrame bitmapFrame = null;
BitmapMetadata metadata = null;
FileInfo originalImage = new FileInfo(imageFlePath);

if (File.Exists(imageFlePath))
{
// load the jpg file with a JpegBitmapDecoder
using (Stream jpegStreamIn = File.Open(imageFlePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
}

bitmapFrame = decoder.Frames[0];
metadata = (BitmapMetadata)bitmapFrame.Metadata;

if (bitmapFrame != null)
{
BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();

if (metaData != null)
{
// modify the metadata
metaData.SetQuery("/app1/ifd/exif:{uint=40092}", comments);

// get an encoder to create a new jpg file with the new metadata.
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
//string jpegNewFileName = Path.Combine(jpegDirectory, "JpegTemp.jpg");

// Delete the original
originalImage.Delete();

// Save the new image
using (Stream jpegStreamOut = File.Open(imageFlePath, FileMode.CreateNew, FileAccess.ReadWrite))
{
encoder.Save(jpegStreamOut);
}
}
}
}
}

This is essentially a lightly modified version of the code found under the link which Konamiman kindly supplied.

Please be aware that to make this work you will need to add .NET references to PresentationCore and WindowsBase. If using Visual Studio 2008, this can be achieved via the following:

  1. Right click on your project in the Solution Explorer

  2. From the drop down list, select Add 'Reference...'

  3. From the new box which opens, select the '.NET' tab

  4. Scroll to the two references mentioned above and on each, click ok

Many thanks to both danbystrom and Konamiman for your help in this matter. I really appreciate the quick response.

Add Comment To JPEG in C#

You will probably have to use some metadata, for example, check out these two sites, MSDN, dreamincode.

How do I add JPEG comment (COM) to an image?

The data type for /com/TextEntry is a bit tricky, it requires an LPSTR. Which is a raw 8-bit encoded string pointer. You can do this by passing a char[] for the argument. Fix:

   meta.SetQuery("/com/TextEntry", "xxx".ToCharArray());

Do note that text encoding might be an issue if you use non-ASCII characters, you'll get text encoded in the machine's default code page (Encoding.Default).

Can you open a JPEG, add text, and resave as a JPEG in .NET?

Something like this:

var filePath = @"D:\Pictures\Backgrounds\abc.jpg";
Bitmap bitmap = null;

// Create from a stream so we don't keep a lock on the file.
using (var stream = File.OpenRead(filePath))
{
bitmap = (Bitmap)Bitmap.FromStream(stream);
}

using (bitmap)
using (var graphics = Graphics.FromImage(bitmap))
using (var font = new Font("Arial", 20, FontStyle.Regular))
{
// Do what you want using the Graphics object here.
graphics.DrawString("Hello World!", font, Brushes.Red, 0, 0);

// Important part!
bitmap.Save(filePath);
}


Related Topics



Leave a reply



Submit