VBA: Why are my photos uploading sideways

PatAccess

Registered User.
Local time
Today, 07:40
Joined
May 24, 2017
Messages
284
Hell Smart people,

I have a form with several photos uploading via a command button. I've made sure the images are already rotated in a portrait position but when I upload them they come out sideways. Anyone can help please?

Thank you,
 
Here you go!

Private Sub cmdAddPhoto1_Click()
Dim objPhoto As Object
Dim strfile As String
Dim strfolder As String
Dim varItem As Variant
Set objPhoto = Application.FileDialog(3)
objPhoto.allowMultiSelect = True
If objPhoto.Show Then
For Each varItem In objPhoto.SelectedItems
strfile = Dir(varItem)
strfolder = Left(varItem, Len(varItem) - Len(strfile))
MsgBox "Folder" & strfolder & vbCrLf & "File: " & strfile
PhotoSource1 = strfolder + strfile
Next
End If
Me.Refresh
Set objPhoto = Nothing
End Sub
 
Nothing stands out in your code
Sorry if this is condescending but did you save after changing the orientation?
Check in Explorer

Also what file format are they? Try changing to a different format e.g. PNG or BMP or GIF
Don't use JPG in Access

Also do read the first link at the bottom of the page
 
I did a web search on "MS Access rotate image" and found some basic ideas:

1. Access does not have an "Image.Orientation" property (or whatever it would be called). Therefore, rotate the image externally before you use it.

2. Find a commercially available image editor OR use automation to activate the image viewer you have to open, rotate, and save the image in question. Or there are some third-party ActiveX controls that will do this.

By itself, Access will not do this for you.
 
Nothing stands out in your code
Sorry if this is condescending but did you save after changing the orientation?
Check in Explorer

Also what file format are they? Try changing to a different format e.g. PNG or BMP or GIF
Don't use JPG in Access

Also do read the first link at the bottom of the page

Hello,
I've tried every other format and it was the same and it seems like it just does not take portrait images. When I cropped that same image as landscape it worked. I'll have to do it for all of them :(

Thank you so the help
 
Sorry it didn't help
Did you read the link I suggested - at the bottom of the page

All I can add is that I have no problem importing / linking images in either orientation and that includes images I have rotated first
 
Post your database with some sample photo, let's see if we've the same problem.
 
PatAccess solved his own problem by cropping the photo. For me too, the image orientation returned to what I was viewing when I cropped the image using a 3rd party software.
Isladogs, I don't see a link at the bottom of the page but would like to know what 'jpg' format is not recommended for Access. I've found it acceptable so far.
 
There used to be a similar threads list at the bottom of each thread but at the moment its missing. See my article:

JPG is a lossy format and results can be poor for JPG images in Access. PNG are a similar size and give better results
 
There used to be a similar threads list at the bottom of each thread but at the moment its missing. See my article:

JPG is a lossy format and results can be poor for JPG images in Access. PNG are a similar size and give better results
This is an old thread but there still seems to be some interest.
I was having the same problem and found that stripping the metadata allowed the images to be displayed in the correct orientation.
A quick search revealed software from devhut to do the job. https://www.devhut.net/removing-image-exif-properties-using-wia-in-vba/
Re Colin's comment on jpg images, it depends.
If jpg's are saved with "No compression" they will be a bit larger but the image quality will not be degraded. This does, of course, depend on how said images were collected. If from an entry level camera and shot in jpg mode the images are pre-processed in camera with little, or no, control from the operator. Images taken in raw mode can be processed and manipulated at will and then saved with, or without, compression.
 
I was able to put together an image display function, but I can't recall where everything came from. It took a lot of fiddling to get it working, and in my case it takes a few seconds to show an image, but it is working now.

It uses WIA - a reference to Windows Image Acquisition library.
It uses the orientation property from the exif property ID 274) - I assume there is a named constant available.

I used this thread for help. I haven't investigated further today but I assume there must be a similar thread somewhere that I did contribute to.


I don't quite follow the code, but I think it makes a temporary copy of the actual image, rotated by whatever is required, and then displays the temporary copy. I remember struggling to get it to work, as I thought my code ought to have worked, but just didn't. I'm pretty sure I asked quite a few questions of @isladogs

I actually put together a database that used the core procedure below to iterate images in a folder of images, rotating each image as necessary.
I haven't uploaded the function mkflt which actually rotates/flips the image

At the moment I have a form which shows a small image of the original file, and a larger picture of the rotated image with navigation controls to step through the images in the target folder.

Code:
Public Sub ProcessImage(infile As String)

Dim txtfile As String
Dim saveimage As String

Dim Image As WIA.ImageFile
Dim ip As WIA.ImageProcess
      
    txtfile = infile
  
    'MsgBox "Processing: " & txtfile
  
    Let Me.ImageOrig.Picture = infile
    Let Me.Image1.Picture = ""
  
    Let Me.TB_Orientation.Value = 1
    Set Image = New WIA.ImageFile
  
    Call Image.LoadFile(infile)
  
    If Image.Properties.Exists("274") Then
        Set ip = New WIA.ImageProcess
        With ip
'            On Error GoTo B:
            On Error GoTo Err_Handler1
          
            Let Me.TB_Orientation.Value = Image.Properties("274").Value
            Select Case Image.Properties("274").Value
                Case 2: Call mkFlt(ip, "FlipHorz")
                Case 3: Call mkFlt(ip, "Rotate", 180)
                Case 4: Call mkFlt(ip, "FlipVert")
                Case 5: Call mkFlt(ip, "FlipHorzandRotate", 270)
                Case 6: Call mkFlt(ip, "Rotate", 90)
                Case 7: Call mkFlt(ip, "FlipHorzandRotate", 90)
                Case 8: Call mkFlt(ip, "Rotate", 270)
            End Select
            Set Image = .Apply(Image)
        End With
    End If
  
B:  On Error GoTo Err_Handler2
    'Me.Image1.PictureData = Image.FileData.BinaryData
 
    'Me.Image1.Picture = Image.FileData.BinaryData
  
    saveimage = CurrentProject.Path & "\" & "temp.jpg"
    On Error Resume Next
    Kill saveimage
  
    Image.SaveFile saveimage
    Me.Image1.Picture = saveimage
  
X:  Exit Sub

E:  Debug.Print Err.Description
    MsgBox "Error: " & Err & "  Dsc: " & Err.Description

Exit Sub

Err_Handler1:
    'MsgBox "Error: " & Err & "  Dsc: " & Err.Description, , "Handler1"
    Resume B:

Err_Handler2:
    MsgBox "Error: " & Err & "  Dsc: " & Err.Description, , "Handler2"


End Sub
 
Last edited:
I was able to put together an image display function, but I can't recall where everything came from. It took a lot of fiddling to get it working, and in my case it takes a few seconds to show an image, but it is working now.
The following code will remove the rotational instruction from EXIF. It actually makes a new copy of the image that displays correctly.
The original code was from Isladogs or dev_hut, I looked at so much I forget where it all came from.
Regarding images taking a while to load, you need to resize them to about 2mb or less. The smaller they are the quicker they load.
See the second code below for resizing.

strPath is the full path to the original image.
the original will be overwritten as the code stands.
change the strpath in "img.savefile" to avoid the overwrite in the "reset" code and "oWIA.SaveFile" in "resize"

Code:
Private Sub ResetOrientation(strPath As String)
    On Error Resume Next
    Dim img As Object, IP As Object
    Set img = CreateObject("WIA.ImageFile") 'create WIA objects
    Set IP = CreateObject("WIA.ImageProcess")
    With IP
        .Filters.Add (.FilterInfos("Exif").FilterID)
        .Filters(1).Properties("ID") = 274    'Orientation    ' = GDIP   "PropertyTagOrientation"
        .Filters(1).Properties("Type") = 1003 'UnsignedIntegerImagePropertyType   ' = Equivalent to GDIP "PropertyTagTypeShort"
        .Filters(1).Properties("Value") = 1 'Normal orientation - possible values = 1-8
        img.LoadFile (strPath) 'load temp image
        Set img = .Apply(img) 'apply changes
        Kill strPath 'delete original image so it can be overwritten
        img.SaveFile (strPath) 'save updated image to original path
    End With
    Set img = Nothing
    Set IP = Nothing
End Sub


nHeight at 1200 and nwidth at 1800 results in an image of about 2mb and standard dimensions.

If saving to a different file, don't forget to remove "Kill strPath"
Code:
Public Sub reSize(strPath As String)
    Dim oWIA    As Object
    Dim oIP     As Object
    Dim nHeight As Integer
    Dim nWidth  As Integer
    Dim sResize As String
    nHeight = 1200
    nWidth = 1800
    Set oWIA = CreateObject("WIA.imagefile")
    oWIA.LoadFile stsPath
    Set oIP = CreateObject("WIA.imageprocess")
    oIP.Filters.Add oIP.FilterInfos("Scale").FilterID
    oIP.Filters(1).Properties("MaximumHeight") = nHeight
    oIP.Filters(1).Properties("MaximumWidth") = nWidth
    Set oWIA = oIP.Apply(oWIA)
    Kill strPath
    oWIA.SaveFile stsPath
    Set oWIA = Nothing
End Sub
 

Users who are viewing this thread

Back
Top Bottom