Solved VBA - Image control, orientation incorrect, get EXIF data (1 Viewer)

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:07
Joined
May 7, 2009
Messages
19,169
When clicked, the temp image is first modified to Orientation = 1 (Normal), then the original image is overwritten.
the original file was not altered (rotated), rather the exif info was updated.
and i think (not tested), some Original property were destroyed (in the exif) when you
put your Orientation property.
 

ironfelix717

Registered User.
Local time
Today, 19:07
Joined
Sep 20, 2019
Messages
193
that is what you think or wish. but phones have "sensors" and knows which is portrait and landscape.
so your wish will never happen.
You missed the point. The user intended to take the photo and view it as the same. Do you take a portrait photo with your cell phone and expect to view it with your head cocked at a 90 degrees? Or Is it just that Landscapes, are acceptable to view at a "normal" angle? Lets give both the photos an EXIF of 1, and rotate either one to match the other. That sounds like a better solution to me. (On contrary to computational efficiency)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:07
Joined
May 7, 2009
Messages
19,169
You missed the point.
like i said human have "thinking" while phones have "sensor".
they don't think as we do. what is portrait to you might not on
the device. maybe in the future phones you can "command" it
to take whatever positiion you held the device as portrait.
 

ironfelix717

Registered User.
Local time
Today, 19:07
Joined
Sep 20, 2019
Messages
193
like i said human have "thinking" while phones have "sensor".
they don't think as we do. what is portrait to you might not on
the device. maybe in the future phones you can "command" it
to take whatever positiion you held the device as portrait.

I understand that, but its not the distinction that should be made.

We are asking the question of:
When we take photo, what is the "normal' orientation (0-degrees)?

And the answer to that is only one single answer, that is defined only by humans:
Whatever way the user intended to view the photo in post.

That does not matter what direction the phone was tilted, what the photo's subject was, when it was taken, where it was taken---

It only matters what the user intended to view it later. That is what the EXIF should be = 1.
 

Nixversteher

New member
Local time
Today, 16:07
Joined
Feb 25, 2018
Messages
16
yes, newer cameras and phones should be able to do this automatically. but the standard exif is a bit older, and back then this was probably not so easy to do due to technology. so the display software has to rotate the images correctly.

hi colin
in your shown example it is cool that the orientation is shown in words and i can read that. but problematic is when these words are used as comparative values programmatically, cue localization. my window language is not english.
and you don't have to save the images. as i said load image with wia, rotate image with wia, then assign the data from the wia directly to the access image control.
 

Attachments

  • WiaRotate.accdb
    412 KB · Views: 163

isladogs

MVP / VIP
Local time
Today, 23:07
Joined
Jan 14, 2017
Messages
18,186
Hi @Nixversteher
I agree that it would be simpler if the orientation was given as an integer between 1 & 8
However, I used the file's extended properties to do this and the EXIF data for orientation is changed to a text value....

NOTE: I use extended properties as my app is designed to work with a variety of images...not just JPG/JPEG

Here are some test photos I created using the letter F:
1628600063191.png


The original photo is the one named WIN_....
I used a modified version of your code to change the EXIF orientation (but didn't rotate or flip the images as well).
So F1 has orientation 1, F2 is orientation 2 etc

This is how the orientation of the 8 images were reported in my app:
1628600152401.png


I believe @arnelgp is probably correct when he stated that whilst the code changes the EXIF value, it doesn't physically alter the image rotation.
For that reason, the images aren't 'helpfully' rotated by File Explorer.
If the files were physically rotated by just modifying the EXIF value, all would appear as F in Explorer.
To complete the process would require using WIA to rotate/flip in addition to changing the EXIF orientation.
Does that make sense?

I'm fully aware that there is no need to save the images. It was done purely for the OP... :rolleyes:
Version 3.2 just rotates the images...and I used WIA for that purpose.

However the OP stated he wanted the images SAVED with the EXIF set to 1 (Normal)
That was why I added that feature as an option in version 3.3 though the OP didn't refer to what I'd done! ;)

Hope that clarifies things for you

Anyway, now I know all the orientation descriptions from 1-8, I'm going to do one further update to include handling all flipped images then upload it as an update to my sample databases thread Folder Image Viewer | Access World Forums (access-programmers.co.uk).
I will post an update here when its ready.

@arnelgp
For info
When modifying images e.g. cropping in Snip & Sketch etc, EXIF data for the camera IS removed
However, changing the EXIF orientation using this code does NOT strip out any of the other EXIF values
 
Last edited:

ironfelix717

Registered User.
Local time
Today, 19:07
Joined
Sep 20, 2019
Messages
193
All,

A lot has been learned about EXIF / Image Orientation for me during the course of the discussion. I believe we have working solutions.
I think this was a bit of a blank space in the forum that we've now filled with good resources.
Thanks to all who contributed to the discussion.

I am providing a function that is a modified conglomerate of aforementioned code resources. It allows the ability to...

- Physically rotate the image
- Modify the EXIF
- Return a WIA Image object


Code:
Public Function RotateImage(Filepath As String, Optional OutputFile As String, _
                            Optional Rotate As Long, Optional Orientation As Integer) As Object
' ACKNOWLEDGMENTS:
'           'This code modification by @IronFelix717 8.10.21
'            Reference Discussion:
'            https://www.access-programmers.co.uk/forums/threads/
'              vba-image-control-orientation-incorrect-get-exif-data.318976/
'            EXIF contribution credits:  @Nixversteher 8.9.21
'            Rotate Image credits:       Daniel Pineault, CARDA Consultants Inc
'                https://cardaconsultants.com/image-manipulation-through-vba/
'
'----------------------------------------------------------------------------------------------
'PURPOSES:  PHSYCIALLY ROTATE PHOTO AND EXPORT
'           MODIFY EXIF ORIENTATION VALUE
'           FUNCTION RETURNS WIA.ImageFile Object

'ARGS:
'           [Filepath]:   target Image
'           [OutputFile]: filename of export [optional]
'                           If not specified, output file will not be produced
'                           If FilePath = OutputFile then overwrite on target image
'           [Rotate]:     degress to rotate [optional]
'                           if not specified, photo will not be roated
'           [Orientation]: EXIF orientation value (1-8) [optional]
'                           if not specified, target EXIF will be preserved
'----------------------------------------------------------------------------------------------
Dim oWIA                  As Object    'WIA.ImageFile
Dim oIP                   As Object    'ImageProcess

On Error GoTo Handler

'Initialize File Obj:
Set oWIA = CreateObject("WIA.ImageFile")
oWIA.LoadFile Filepath
Set RotateImage = oWIA 'default

If Not (Rotate = 0 And Orientation = 0) Then
    Set oIP = CreateObject("WIA.ImageProcess")
   
    With oIP
        'EXIF DATA
        If Orientation > 0 Then
            .Filters.Add (.FilterInfos("Exif").FilterID)
            .Filters(1).Properties("ID") = 274      'Orientation = GDIP "PropertyTagOrientation"
            .Filters(1).Properties("Type") = 1003   'UnsignedIntegerImagePropertyType
            .Filters(1).Properties("Value") = Orientation     '1-8
        End If
       
        'ROTATE IMG
        .Filters.Add .FilterInfos("RotateFlip").FilterID
        .Filters(.Filters.count).Properties("RotationAngle") = Rotate
    End With
   
    Set oWIA = oIP.Apply(oWIA)
   
    If OutputFile <> "" Then
        If Filepath = OutputFile _
           And Dir(OutputFile) <> "" Then Kill (OutputFile)
        oWIA.SaveFile OutputFile 'Export
    End If
   
    Set RotateImage = oWIA 'Return WIA Object with changes
End If

Exit Function
Handler:
    MsgBox "Error:  RotateImage() Failed: " & vbCrLf & Err.Number & Err.Description
End Function


And it's uses can be demonstrated here...

Code:
Private sub Test()
'IronFelix717 https://www.access-programmers.co.uk/
Dim fpath           As String
Dim img             As WIA.ImageFile

fpath = *your path to file*

'ROTATING THE TARGET IMAGE, MODIFYING EXIF, AND WRITING TO DISK (OVERWRITE):
Set img = RotateImage(fpath, fpath, 90, 1)

'ROTATING THE TARGET IMAGE AND ASSIGNING DATA TO A CONTROL:
Set img = RotateImage(fpath, , 90)
objPreview.PictureData = img.FileData.BinaryData

'SIMPLY RETURNING IMG FILE OBJECT (MIS-USE OF FUNCTION)
Set img = RotateImage(fpath)
objPreview.PictureData = img.FileData.BinaryData
End Sub


Thanks to all
 

Users who are viewing this thread

Top Bottom