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.
LVL 1
JedNebulaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John TsioumprisSoftware & Systems EngineerCommented:
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.
0
JedNebulaAuthor Commented:
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
0
John TsioumprisSoftware & Systems EngineerCommented:
Is this the image you are having issues? do you get it rotated? i
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

John TsioumprisSoftware & Systems EngineerCommented:
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
0
JedNebulaAuthor Commented:
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.
0
ktaczalaCommented:
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.
0
ArkCommented:
First, Exif info doesn't rotate image. It just informs that original image was rotated and this exif tag says that application should rotate this image. Some applications (most browsers, picassa photo viewer and othres) understand this tag and rotate image accordingly, some others - no and shows image as it is. So you need rotate image yourself and set orientation to 1 (Normal) to allow all applications show image properly:
Private Sub Command1_Click()
   'Dim img As New ImageFile
   'Dim imgProc As New ImageProcess
   Const UnsignedIntegerImagePropertyType = 1003
   Dim Img, imgProc
   Set Img = CreateObject("WIA.ImageFile")
   Set imgProc = CreateObject("WIA.ImageProcess")
   
   Img.LoadFile "rotated_product_image.JPG"
   Dim n As Long
   n = Img.Properties("Orientation")
   If n = 1 Then Exit Sub
   imgProc.Filters.Add imgProc.FilterInfos("RotateFlip").FilterID
   Select Case Img.Properties("Orientation")
       Case 1 'Normal, no need to process
       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 'Orientation tag
      imgProc.Filters(2).Properties("Type") = UnsignedIntegerImagePropertyType
      imgProc.Filters(2).Properties("Value") = 1
      Set Img = imgProc.Apply(Img)
      Img.SaveFile "NormalImage.jpg"
   End If
   Set Img = Nothing
   Set imgProc = Nothing
End Sub

Open in new window

PS. If you need save rotated image directly to Database without saving to file you can use
rec("ImageData") = img.FileData.BinaryData
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JedNebulaAuthor Commented:
Perfect answer - everything I needed. Thank you!
0
ArkCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.