Saving Jpeg/Exif info Please Help.

T

TheGanjaMan

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...
 
Z

zacks

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.
 
J

james

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
 
G

gene kelley

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/scripts/ShowCode.asp?txtCodeId=48293&lngWId=1

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

Gene
 
T

TheGanjaMan

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
 
T

TheGanjaMan

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
 

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