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

Saving Jpeg/Exif info Please Help.

P: n/a
Hi everyone,

I'm trying to write up a simple image stamper application that stamps the
Exif date information from the jpegs that I've taken from my digital
camera and saves the new file with the date stamped on the lower right
part of the picture. (I'm not an advanced programmer so my code may not
be 100% efficient - sorry, I'm still learning)

Everything works fine until the saving part. I've been able to read the
file into a picturebox, transform it into bitmap to write the text on it,
Then save the file under a new filename.

But when I save the file all Exif information is lost. How can I
preserve this information? I'm thinking that when I transform the image
to a bitmap this loss happens, but When I work on the image itself using
the image.fromfile property the file is locked until the application
exits so I have to read the file as a filestream and convert it into a
bitmap to write text on it and save it without locking the file.
Otherwise I cannot overwrite the file.

I'm doing something along these lines:
(I have 2 pictureboxes pb_orig and pb_final, the Pictext is the Exif date
that is returned from another function)

Dim fs As New System.IO.FileStream(PicName, IO.FileMode.Open)
Dim bm As New Bitmap(fs)
Dim g As Graphics = Graphics.FromImage(bm)
Dim ph As Integer = pb_Orig.Image.Height
Dim pw As Integer = pb_Orig.Image.Width
Dim StrWidth = g.MeasureString(PicText.ToString, New Font
(TS_CB_Font.SelectedText, (pw * 0.05), FontStyle.Regular,
GraphicsUnit.Pixel)).Width
Dim StrHeight = g.MeasureString(PicText.ToString, New Font
(TS_CB_Font.SelectedText, (ph * 0.05), FontStyle.Regular,
GraphicsUnit.Pixel)).Height
g.DrawString(PicText, New Font(TS_CB_Font.Text, (ph * 0.05),
FontStyle.Regular, GraphicsUnit.Pixel), New SolidBrush(CD1.Color),
pb_Orig.Image.Width - StrWidth, pb_Orig.Image.Height - StrHeight)
pb_Final.Image = DirectCast(bm.Clone, Image)
fs.Close()
bm.Dispose()
The save goes like this:
Dim SaveBitmap As New Bitmap(pb_Final.Image)
SaveBitmap.Save(SaveFileDialog1.FileName, Imaging.ImageFormat.Jpeg)
SaveBitmap.Dispose()

If I dont use the Imaging.ImageFormat.Jpeg somehow my pictures end up
being saved as PNG files...

Any help will be greatly appreciated...
Apr 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I'm guessing here, but I'm thinking when you convert it to bitmap, you
lose all of the EXIF data which is specific to JPG format files.

Apr 11 '06 #2

P: n/a
Check out this link: http://www.bobpowell.net/modifyImage.htm

The code sample is written in C# but, can be converted to VB pretty easy
(sample below):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim imageName As String = "c:\\Test.jpg" ' substitute your own image name
here

'NOTE Don't use this test code to draw on images that you want to keep!

Dim i As Image = Image.FromFile(imageName)

Dim g As Graphics = Graphics.FromImage(i)

g.FillRectangle(Brushes.White, 5, 5, 170, 40)

'g.DrawRectangle(Pens.DarkBlue, 5, 5, 150, 30)

Dim sf As StringFormat = CType(StringFormat.GenericTypographic.Clone(),
StringFormat)

sf.Alignment = StringAlignment.Center

sf.LineAlignment = StringAlignment.Center

g.DrawString(DateTime.Now.ToShortDateString(), New Font("Arial", 14,
GraphicsUnit.Point), Brushes.Black, 5, 5) ', New RectangleF(7, 7, 94, 25),
sf)

g.Dispose()

i.Save("C:\test2.jpg")

i.Dispose()

'----------------------------------------------------------------

' Converted from C# to VB .NET using CSharpToVBConverter(1.2).

' Developed by: Kamal Patel (http://www.KamalPatel.net)

'----------------------------------------------------------------

End Sub

What I'm trying to say is you can draw on a jpg image and should save the
image data. The reason you are loosing the EXIF data is because you are
drawing to a Bitmap and then saving back to jpg. The conversion between the
two is causing the loss of info. By just drawing on the jpg image like I did
using Bob's code ( converted from C# to VB.NET) I was able to draw the
ShortDate info on the image and save the image (with another name) without
loosing the EXIF data.

james

"TheGanjaMan" <th*********@somplace.invalid> wrote in message
news:Xn*************************@207.46.248.16...
Hi everyone,

I'm trying to write up a simple image stamper application that stamps the
Exif date information from the jpegs that I've taken from my digital
camera and saves the new file with the date stamped on the lower right
part of the picture. (I'm not an advanced programmer so my code may not
be 100% efficient - sorry, I'm still learning)

Everything works fine until the saving part. I've been able to read the
file into a picturebox, transform it into bitmap to write the text on it,
Then save the file under a new filename.

But when I save the file all Exif information is lost. How can I
preserve this information? I'm thinking that when I transform the image
to a bitmap this loss happens, but When I work on the image itself using
the image.fromfile property the file is locked until the application
exits so I have to read the file as a filestream and convert it into a
bitmap to write text on it and save it without locking the file.
Otherwise I cannot overwrite the file.

I'm doing something along these lines:
(I have 2 pictureboxes pb_orig and pb_final, the Pictext is the Exif date
that is returned from another function)

Dim fs As New System.IO.FileStream(PicName, IO.FileMode.Open)
Dim bm As New Bitmap(fs)
Dim g As Graphics = Graphics.FromImage(bm)
Dim ph As Integer = pb_Orig.Image.Height
Dim pw As Integer = pb_Orig.Image.Width
Dim StrWidth = g.MeasureString(PicText.ToString, New Font
(TS_CB_Font.SelectedText, (pw * 0.05), FontStyle.Regular,
GraphicsUnit.Pixel)).Width
Dim StrHeight = g.MeasureString(PicText.ToString, New Font
(TS_CB_Font.SelectedText, (ph * 0.05), FontStyle.Regular,
GraphicsUnit.Pixel)).Height
g.DrawString(PicText, New Font(TS_CB_Font.Text, (ph * 0.05),
FontStyle.Regular, GraphicsUnit.Pixel), New SolidBrush(CD1.Color),
pb_Orig.Image.Width - StrWidth, pb_Orig.Image.Height - StrHeight)
pb_Final.Image = DirectCast(bm.Clone, Image)
fs.Close()
bm.Dispose()
The save goes like this:
Dim SaveBitmap As New Bitmap(pb_Final.Image)
SaveBitmap.Save(SaveFileDialog1.FileName, Imaging.ImageFormat.Jpeg)
SaveBitmap.Dispose()

If I dont use the Imaging.ImageFormat.Jpeg somehow my pictures end up
being saved as PNG files...

Any help will be greatly appreciated...

Apr 11 '06 #3

P: n/a
On Tue, 11 Apr 2006 05:53:44 -0700, TheGanjaMan
<th*********@somplace.invalid> wrote:
Hi everyone,

I'm trying to write up a simple image stamper application that stamps the
Exif date information from the jpegs that I've taken from my digital
camera and saves the new file with the date stamped on the lower right
part of the picture. (I'm not an advanced programmer so my code may not
be 100% efficient - sorry, I'm still learning)

Everything works fine until the saving part. I've been able to read the
file into a picturebox, transform it into bitmap to write the text on it,
Then save the file under a new filename.

But when I save the file all Exif information is lost. How can I
preserve this information? I'm thinking that when I transform the image
to a bitmap this loss happens, but When I work on the image itself using
the image.fromfile property the file is locked until the application
exits so I have to read the file as a filestream and convert it into a
bitmap to write text on it and save it without locking the file.
Otherwise I cannot overwrite the file.

I'm doing something along these lines:
(I have 2 pictureboxes pb_orig and pb_final, the Pictext is the Exif date
that is returned from another function)

Dim fs As New System.IO.FileStream(PicName, IO.FileMode.Open)
Dim bm As New Bitmap(fs)
Dim g As Graphics = Graphics.FromImage(bm)
Dim ph As Integer = pb_Orig.Image.Height
Dim pw As Integer = pb_Orig.Image.Width
Dim StrWidth = g.MeasureString(PicText.ToString, New Font
(TS_CB_Font.SelectedText, (pw * 0.05), FontStyle.Regular,
GraphicsUnit.Pixel)).Width
Dim StrHeight = g.MeasureString(PicText.ToString, New Font
(TS_CB_Font.SelectedText, (ph * 0.05), FontStyle.Regular,
GraphicsUnit.Pixel)).Height
g.DrawString(PicText, New Font(TS_CB_Font.Text, (ph * 0.05),
FontStyle.Regular, GraphicsUnit.Pixel), New SolidBrush(CD1.Color),
pb_Orig.Image.Width - StrWidth, pb_Orig.Image.Height - StrHeight)
pb_Final.Image = DirectCast(bm.Clone, Image)
fs.Close()
bm.Dispose()
The save goes like this:
Dim SaveBitmap As New Bitmap(pb_Final.Image)
SaveBitmap.Save(SaveFileDialog1.FileName, Imaging.ImageFormat.Jpeg)
SaveBitmap.Dispose()

If I dont use the Imaging.ImageFormat.Jpeg somehow my pictures end up
being saved as PNG files...

Any help will be greatly appreciated...


Concerning the EXIF tags:
You would need to read and temporarily store the EXIF tags when
loading the original and then write back the tags when resaving the
altered file (not a trivial matter).

If you happen to have VB6, you could look at this example:
http://www.Planet-Source-Code.com/vb...48293&lngWId=1

Or, try a Google seach for a similar example in .Net.

Gene
Apr 12 '06 #4

P: n/a
Hi James,

Thank you for the link and the code sample...
It seems to have solved my problems for now and pointed me in the right direction.

Cheers...
TheGanjaMan

"james" <jj***************@earthlink.net> wrote in
news:er**************@TK2MSFTNGP02.phx.gbl:
Check out this link: http://www.bobpowell.net/modifyImage.htm

The code sample is written in C# but, can be converted to VB pretty
easy (sample below):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim imageName As String = "c:\\Test.jpg" ' substitute your own image
name here

'NOTE Don't use this test code to draw on images that you want to
keep!
What I'm trying to say is you can draw on a jpg image and should save
the image data. The reason you are loosing the EXIF data is because
you are drawing to a Bitmap and then saving back to jpg. The
conversion between the two is causing the loss of info. By just
drawing on the jpg image like I did using Bob's code ( converted from
C# to VB.NET) I was able to draw the ShortDate info on the image and
save the image (with another name) without loosing the EXIF data.

james


Apr 12 '06 #5

P: n/a
Hi Gene,

Thanks for the link.
I am able to extract and store all the required exif tags in memory when
working with the picture. I just didn't know how to write them back into
the file.

Thanks for helping out...

Cheers...
TheGanjaMan

gene kelley <ok**@by.me> wrote in
news:g1********************************@4ax.com:
Concerning the EXIF tags:
You would need to read and temporarily store the EXIF tags when
loading the original and then write back the tags when resaving the
altered file (not a trivial matter).

If you happen to have VB6, you could look at this example:
http://www.Planet-Source-Code.com/vb...?txtCodeId=482
93&lngWId=1

Or, try a Google seach for a similar example in .Net.

Gene


Apr 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.