Link to home
Create AccountLog in
Avatar of JedNebula
JedNebulaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Remove Exif Orientation before adding image to db

We use the following VB6 function to add images to a SQL database.

Public Function AddJPEGImageToDatabase(strPath As String, Optional ImageID As Long = -1) As Long
    Dim rec As New ADODB.Recordset
    Dim picStream As New ADODB.Stream
    Dim conn As ADODB.Connection
    Dim strSQL As String
    Dim Pic As StdPicture

        Set conn = GetADOConnectionObject
        strSQL = "SELECT * FROM dbo.ImageData WHERE ImageID = " & ImageID
        rec.Open strSQL, conn, adOpenDynamic, adLockOptimistic
        Set Pic = LoadPicture(strPath)
        With picStream
            .Mode = adModeReadWrite
            .Type = adTypeBinary
            Call .LoadFromFile(strPath)
            .Position = 0
            If ImageID = -1 Then rec.AddNew
            rec("ImageData") = .Read
            rec("ImageX") = (Pic.Width / 1000) * (567 / Screen.TwipsPerPixelX)
            rec("ImageY") = (Pic.Height / 1000) * (567 / Screen.TwipsPerPixelY)
            strPath = GetRightBit(strPath, "\")
            strPath = Replace(strPath, " ", "-")
            strPath = ReplaceIllegalKeystrokes(strPath, True, True)
            rec("ImageFileName") = strPath
            AddJPEGImageToDatabase = rec("ImageID")
            Set rec = Nothing
            Set Pic = Nothing
            Set picStream = Nothing
        End With

End Function

Open in new window

Recently, we have had a few reports of users saying that images they have taken with their own digital cameras are importing into our software rotated 90 degrees. We since found out that is down to the EXIF data stored in the jpeg by the camera.

Up until now, we have been able to fob the clients off with a utility to remove this EXIF data themselves before importing, but it is starting to require a better solution now.

I have done a little bit of Googling about rotating a StdPicture object in VB6 and haven't been able to find a simple solution. Most of the suggestions include drawing the image manually on the form (but rotated). However, as I am hoping to store the image in the database (with the correct orientation) I think I either need to rotate the actual stdPicture, or remove the EXIF orientation tag before loading.

Thoughts welcome & appreciated.
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

There seems to be a project on Planet-Source-Code that supports reading/changing of  Exif Tag...i suppose with a little work you could change it to your needs.
Avatar of JedNebula


Thank you John. I had a look at that.

For the image I have been testing with though - it didn't seem to find any EXIF data!

I've attached my tester image / project in case it helps.
Is this the image you are having issues? do you get it rotated? i
If you go to this site and read the EXIF you will get that the image does has EXIF info to rotate "Rotate 90 CW"..
I have found this project here that reads EXIF...i have tested it with your image and it does read orientation =6
Ok thanks John, but I have already got that far. Not sure if you saw my project? That also reports the orientation correctly at 6.

The issue I have now is either saving the image to the database, "the proper way up" OR actually removing the EXIF info so that it doesn't load in as rotated.
I would suggest writing code to check and rotate the image when image is viewed.  I am assuming you have an app that not only saves image to DB but also retrieves and views it. (Sorry I don't have VB6 to view your code.) has bultin rotate functions.
Avatar of Ark
Flag of Russian Federation image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Perfect answer - everything I needed. Thank you!
Glad I could help. If you add reference to "Microsoft Windows Image Acquisition Library v2.0" (C:\Windows\System32\wiaaut.dll) you can use:
Public Function LoadRotatedFile(ByVal path As String) As WIA.ImageFile
   Dim img As New WIA.ImageFile
   img.LoadFile path
   If img.Properties("Orientation") > 1 Then 'rotation required
      Dim imgProc As New WIA.ImageProcess
      imgProc.Filters.Add imgProc.FilterInfos("RotateFlip").FilterID
      Select Case img.Properties("Orientation")
         Case 2 'Mirror horizontal
             imgProc.Filters(1).Properties("FlipHorizontal") = True
         Case 3 'Rotate 180
             imgProc.Filters(1).Properties("RotationAngle") = 270
         Case 4 'Mirror vertical
             imgProc.Filters(1).Properties("FlipVertical") = True
         Case 5 'Mirror horizontal and rotate 270 CW
             imgProc.Filters(1).Properties("FlipHorizontal") = True
             imgProc.Filters(1).Properties("RotationAngle") = 270
         Case 6 'Rotate 90 CW
             imgProc.Filters(1).Properties("RotationAngle") = 90
         Case 7 'Mirror horizontal and rotate 90 CW
             imgProc.Filters(1).Properties("FlipHorizontal") = True
             imgProc.Filters(1).Properties("RotationAngle") = 90
         Case 8 'Rotate 270 CW
             imgProc.Filters(1).Properties("RotationAngle") = 270
      End Select
      If imgProc.Filters.Count > 0 Then
         'append filter to change exif tag
         imgProc.Filters.Add imgProc.FilterInfos("Exif").FilterID
         imgProc.Filters(2).Properties("ID") = &H112
         imgProc.Filters(2).Properties("Type") = WIA.UnsignedIntegerImagePropertyType
         imgProc.Filters(2).Properties("Value") = 1
         Set img = imgProc.Apply(img)
      End If
   End If
   Set LoadRotatedFile = img
End Function

Open in new window

Dim img As WIA.ImageFile
Set img = LoadRotatedFile(path)
'Save to disc:
img.SaveFile newPath
'or save to DB:
rec("ImageData") = img.FileData.BinaryData
'or show in PictureBox:
Set Picture1.Picture = img.FileData.Picture

Open in new window

PS. If your app is front-end app you can use reverse way - saving picture "as is" and rotate when displaing from DB:
Dim img As New WIA.ImageFile
Dim v As New WIA.Vector
v.BinaryData = rec("ImageData")
Set img.FileData = v
'Perform rotation if need
Set Picture1.Picture = img.FileData.Picture

Open in new window