Auto Rotate Photos

Tophan

Registered User.
Local time
Today, 06:30
Joined
Mar 27, 2011
Messages
388
Good morning,

Following up on a question previously posted (and solved) in the Tables forum on how to import a file path batch, I have encountered a problem. Using the VBA code provided by @Pat Hartman and @theDBguy, I was able to import all file paths for photos from a Windows Explorer folder. However, I have noticed that any photo taken in portrait mode is rotated to landscape in the report. Is there a way to prevent this from happening?

I was scouring through the various threads on this website and saw something about WIA automation but I'm not sure where to insert that code and how it would affect the import code.

Thanking you in advance for any help provided.
 
copy this code in module:
Code:
Public Function WIA_RotateImage(sInitialImage As String, _
                                lRotAng As Long, _
                                Optional sOutputImage As String) As Boolean
    On Error GoTo Error_Handler

    Dim Img As Object, IP As Object
    Dim ext As String, tfSameFile As Boolean
    Set Img = CreateObject("WIA.ImageFile")
    Set IP = CreateObject("WIA.ImageProcess")

    IP.Filters.Add IP.FilterInfos("RotateFlip").FilterID
    IP.Filters(1).Properties("RotationAngle") = lRotAng

    Img.LoadFile sInitialImage
    Set Img = IP.Apply(Img)

    tfSameFile = (sOutputImage = "") Or (sInitialImage = sOutputImage)
    If tfSameFile Then
        ext = getFileExt(sInitialImage)
        sOutputImage = Replace$(sInitialImage, ext, "") & "_tmp$" & ext
    End If

    Img.SaveFile sOutputImage
    WIA_RotateImage = True
    
    If tfSameFile Then
        Kill sInitialImage
        Name sOutputImage As sInitialImage
    End If
Exit_Procedure:
    Exit Function

Error_Handler:
    WIA_RotateImage = False
    Resume Exit_Procedure
End Function


Private Function getFileExt(ByVal sImage As String) As String
Dim i As Integer
i = InStrRev(sImage, ".")
If i <> 0 Then
    getFileExt = Mid$(sImage, i)
End If
End Function

then check which image has wrong orientation (landscape) and run the function against that image.
example:

Code:
sImage = Environ$("userprofile") & "\pictures\ssms21_02.png"
Call WIA_RotateImage(sImage, 270)
 
Good morning. Sorry for not responding sooner - I was out of office for the majority of yesterday.

I'm trying to understand how get this to work. I saved the module but how do I select the specific photo? Do I go back to the form and select it there? I have little experience with modules so I'm not sure how to run this.
 
Hi,

Just following up on the above request. Can someone help me with this?
 
You get the path to the image and call that code as shown in the example.
I made sure to use the same file.

Here is a quick test I used.
Code:
Private Sub Command2_Click()
Dim strPic As String, strPath As String
Dim blnRotate As Boolean
strPath = "f:\Temp\"
strPic = Me.Image0.Picture
blnRotate = WIA_RotateImage(strPath & strPic, 90, strPath & strPic)
Me.Image0.Picture = strPic
End Sub
 
Hi,

I'm getting a compile error when I try to run this - "Expected variable or procedure, not module" and the below line of the code is highlighted.

[blnRotate = WIA_RotateImage(strPath & strPic, 90, strPath & strPic)]
 
Firstly, you cannot name a module the same as a function or sub. :(

How is Access meant to know which one you are talking about?
Always show the whole code, even if you have to show how you changed the code offered by anyone.

Why is that code enclosed in [ & ] ?
 
@arnelgp
Why the discrepancy in the image, when form first loads?
I can turn the image, but when I close the form and reopen, I have my original image again?

Actual Image
1750787615487.png


Initial Load of form
1750787647069.png

After Turn
1750787693993.png

And file
1750787729389.png


Closing and reopening form.
1750787799979.png
 
@arnelgp
Why the discrepancy in the image, when form first loads?
I can turn the image, but when I close the form and reopen, I have my original image again?

Actual Image
View attachment 120336

Initial Load of form
View attachment 120337
After Turn
View attachment 120338
And file
View attachment 120339

Closing and reopening form.
View attachment 120340

@arnelgp
Why the discrepancy in the image, when form first loads?
I can turn the image, but when I close the form and reopen, I have my original image again?

Actual Image
View attachment 120336

Initial Load of form
View attachment 120337
After Turn
View attachment 120338
And file
View attachment 120339

Closing and reopening form.
View attachment 120340
Hi. This is the full code I saved to my database.
Code:
Private Sub Command55_Click()
Dim strPic As String, strPath As String
Dim blnRotate As Boolean
strPath = "c:\Users\"
strPic = Me.Image23.Picture
blnRotate = WIA_RotateImage(strPath & strPic, 90, strPath & strPic)
Me.Image23.Picture = strPic
End Sub

The error message is occurring on Line 6. My form was previously a continuous form. I changed it to a single form and added a button with the code you provided added to the On Click event, hoping I could rotate an individual photo.

I'm trying one more tweak - I'll let you know if it works or not.
 
Show your module list. I suspect you have created one with the same name as the function.
Rename the module to modWIA_RotateImage if you have.
 
Ok. I made the change. No error message this time but the image is not rotating either.
 
Upload your db, at least enough to see the issue.
I am off to bed, so if @arnelgp does not jump in, I will have a look tomorrow.
 
Please test your photos using my free Folder Image Viewer app and see if that helps

 
Hi,

I've attached the DB. It's not much, I was just trying to get the codes and modules to work so the data in there is test data. Nearly all the pictures in the current form were taken in portrait orientation and are showing as landscape in the form and report.

I'm not sure if the modWIA_Rotation is being executed. I don't understand how to run it.
 

Attachments

Hi,

I've attached the DB. It's not much, I was just trying to get the codes and modules to work so the data in there is test data. Nearly all the pictures in the current form were taken in portrait orientation and are showing as landscape in the form and report.

I'm not sure if the modWIA_Rotation is being executed. I don't understand how to run it.
I do not even see where you are showing a picture in that DB? :(
OK, found it.
 

Attachments

Last edited:

Users who are viewing this thread

Back
Top Bottom