By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,993 Members | 1,984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,993 IT Pros & Developers. It's quick & easy.

Modifying JPEG Comment

P: n/a
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[i] =
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?
Nov 1 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
"Joe Cool" <jo*****@home.netwrote in message
news:b0********************************@4ax.com...
>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[i] =
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?
Nov 1 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.