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:
Right click on your project in the Solution Explorer
From the drop down list, select Add 'Reference...'
From the new box which opens, select the '.NET' tab
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
I am Getting Failed to Load Resource: Net::Err_Blocked_By_Client with Google Chrome
.Net Core Di, Ways of Passing Parameters to Constructor
Struggling Trying to Get Cookie Out of Response with Httpclient in .Net 4.5
What Exactly Is an Assembly in C# or .Net
How to Set Up Httpcontent for My Httpclient Postasync Second Parameter
How to Pass Multiple Enum Values in C#
How to Remove New Line Characters from a String
Units of Measure in C# - Almost
Single Click Edit in Wpf Datagrid
How to Create a More User-Friendly String.Format Syntax
Get a List of Distinct Values in List
Convert Wpf (Xaml) Control to Xps Document
List Sort Based on Another List
Open Source Cad Drawing (Dwg) Library in C#
Could We Save Delegates in a File (C#)
Reference Type Still Needs Pass by Ref