469,644 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

Modifying JPEG Comment

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
1 3039
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.

Similar topics

1 post views Thread by Eugene Morozov | last post: by
10 posts views Thread by Ryan Cooper | last post: by
4 posts views Thread by Dennis Marks | last post: by
9 posts views Thread by BluDog | last post: by
10 posts views Thread by rtilley | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.