Modifying JPEG Comment

J

Joe Cool

I am attempting to add a function to an application I am working on to
modify the JPEG Comment in a Jpeg image file.

I can retrieve the JPEG Comment with no problem. The problem is
modifying it.

I have the contents of a Jpeg loaded into an Image object, _Image,
using the Image.FromFile method.

I convert the Text property of a TextBox to a byte array with code
like this:

Byte[] byteArr = new Byte[txtDescription.Text.Length + 1];

for (int i = 0; i < txtDescription.Text.Length; ++i)
byteArr =
Convert.ToByte(Convert.ToChar(txtDescription.Text.Substring(i, 1)));
byteArr[txtDescription.Text.Length] =
Convert.ToByte(Convert.ToChar("\0"));

I load the current JPEG Comment with:

PropertyItem prop;

prop = _Image.GetPropertyItem(0x9286);

I modify that with:

prop.Len = byteArr.GetUpperBound(0) + 1;
prop.Value = byteArr;

I set the Jpeg Comment in the Image in memory with:

_Image.SetPropertyItem(prop);

I then save the image to a new file with:

FileStream myWriter;

myWriter = new FileStream("c:\\temp.JPG", FileMode.Create,
FileAccess.Write);
_Image.Save(myWriter, ImageFormat.Jpeg);
myWriter.Close();
myWriter.Dispose();

Sometimes it works. But everytime I try to modify the value I was able
to change it to to something else, it always reverts to the original
value, "LEAD Technologies, Inc. V1.0".

I have dumped the byte array, byteArr, to the debug window and it
always appears to be exactly what I am expecting it to be.

Any ideas?
 
B

Bela Istok

I don't have an answer for you JPEG problem, but I have one comment respect
you code, you can get the byte array using a single line like this:

byte [] byteArr = Encoding.ASCII.GetBytes(txtDescription.Text);

The Encoding Class is in the namespace System.Text, and you can replace
ASCII with UTF8, UTF7, UTF32, Unicode, BigEndianUnicode. If you like to use
other handling for you text.

Regards,

Bela Istok
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top