We help IT Professionals succeed at work.

Remove Exif Orientation before adding image to db

JedNebula
JedNebula asked
on
627 Views
Last Modified: 2017-03-30
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.
Comment
Watch Question

John TsioumprisIT Supervisor
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.

Author

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
John TsioumprisIT Supervisor
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Is this the image you are having issues? do you get it rotated? i
John TsioumprisIT Supervisor
CERTIFIED EXPERT
Distinguished Expert 2019

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

Author

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.
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Perfect answer - everything I needed. Thank you!
Ark
CERTIFIED EXPERT

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions