Avatar of JedNebula
JedNebula
Flag 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
          
LoadThePicture:
        Set Pic = LoadPicture(strPath)
          
        With picStream
            .Mode = adModeReadWrite
            .Type = adTypeBinary
            .Open
            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)
            
SetTheFileName:
            rec("ImageFileName") = strPath
            
            rec.Update
            
            .Close
            AddJPEGImageToDatabase = rec("ImageID")
            rec.Close
            
            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.
Visual Basic Classic* vb6

Avatar of undefined
Last Comment
Ark

8/22/2022 - Mon
John Tsioumpris

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

ASKER
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.
ExifReader.cls
Form1.frm
Project1.vbp
rotated_product_image.JPG
John Tsioumpris

Is this the image you are having issues? do you get it rotated? i
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
John Tsioumpris

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
JedNebula

ASKER
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.
ktaczala

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.) VB.net has bultin rotate functions.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Ark

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
JedNebula

ASKER
Perfect answer - everything I needed. Thank you!
Ark

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

Using:
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