VBA – WIA – Flip an Image

I was helping out in a forum discussion regarding manipulating images, specifically flipping them and published some of my code there, so I thought I’d share it here for everyone else to benefit from.

Once again, WIA has everything we need to quickly and easily perform this task! WIA offers developers 2 image processing properties: FlipHorizontal and FlipVertical that we need only apply.

Below are a few sample functions illustrating how it can easily be implemented.
 

Flipping Images Horizontally

'---------------------------------------------------------------------------------------
' Procedure : WIA_FlipHorizontalImage
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Flip an image horizontally using WIA
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Windows Image Acquisition (WIA)
'             https://msdn.microsoft.com/en-us/library/windows/desktop/ms630368(v=vs.85).aspx
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sInitialImage : Fully qualified path and filename of the original image to resize
' sOutputImage  : Fully qualified path and filename of where to save the new image
'
' Usage:
' ~~~~~~
' Call WIA_FlipHorizontalImage("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", _
'                              "C:\Users\MyUser\Desktop\Chrysanthemum_small.jpg")
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2017-01-18              Initial Release
' 2         2018-09-20              Updated Copyright
'---------------------------------------------------------------------------------------
Public Function WIA_FlipHorizontalImage(sInitialImage As String, _
                                        sOutputImage As String) As Boolean
    On Error GoTo Error_Handler
    Dim oWIA                  As Object    'WIA.ImageFile
    Dim oIP                   As Object    'ImageProcess
 
    'Should check if the output file already exists and if so,
    'prompt the user to overwrite it or not
    
    Set oWIA = CreateObject("WIA.ImageFile")
    Set oIP = CreateObject("WIA.ImageProcess")
 
    oIP.Filters.Add oIP.FilterInfos("RotateFlip").FilterID
    oIP.Filters(1).Properties("FlipHorizontal") = True
 
    oWIA.LoadFile sInitialImage
    Set oWIA = oIP.Apply(oWIA)
    oWIA.SaveFile sOutputImage
    WIA_FlipHorizontalImage = True
 
Error_Handler_Exit:
    On Error Resume Next
    If Not oIP Is Nothing Then Set oIP = Nothing
    If Not oWIA Is Nothing Then Set oWIA = Nothing
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: WIA_FlipHorizontalImage" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

 

Flipping Images Vertically

'---------------------------------------------------------------------------------------
' Procedure : WIA_FlipVerticalImage
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Flip an image vertically using WIA
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Windows Image Acquisition (WIA)
'             https://msdn.microsoft.com/en-us/library/windows/desktop/ms630368(v=vs.85).aspx
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sInitialImage : Fully qualified path and filename of the original image to resize
' sOutputImage  : Fully qualified path and filename of where to save the new image
'
' Usage:
' ~~~~~~
' Call WIA_FlipVerticalImage("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", _
'                            "C:\Users\MyUser\Desktop\Chrysanthemum_small.jpg")
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2017-01-18              Initial Release
' 2         2018-09-20              Updated Copyright
'---------------------------------------------------------------------------------------
Public Function WIA_FlipVerticalImage(sInitialImage As String, _
                                      sOutputImage As String) As Boolean
    On Error GoTo Error_Handler
    Dim oWIA                  As Object    'WIA.ImageFile
    Dim oIP                   As Object    'ImageProcess
 
    'Should check if the output file already exists and if so,
    'prompt the user to overwrite it or not
    
    Set oWIA = CreateObject("WIA.ImageFile")
    Set oIP = CreateObject("WIA.ImageProcess")
 
    oIP.Filters.Add oIP.FilterInfos("RotateFlip").FilterID
    oIP.Filters(1).Properties("FlipVertical") = True
 
    oWIA.LoadFile sInitialImage
    Set oWIA = oIP.Apply(oWIA)
    oWIA.SaveFile sOutputImage
    WIA_FlipVerticalImage = True
 
Error_Handler_Exit:
    On Error Resume Next
    If Not oIP Is Nothing Then Set oIP = Nothing
    If Not oWIA Is Nothing Then Set oWIA = Nothing
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: WIA_FlipVerticalImage" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

 

Flipping Both Ways

As always, don’t forget that although I made 2 distinct functions, with WIA you can chain the operations together so if the need be, you can perform both operations in the same function by simply adding the extra property(ies). So you might end up with something along the lines of

'---------------------------------------------------------------------------------------
' Procedure : WIA_FlipHorizontalAndVerticalImage
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Flip an image horizontally and vertically using WIA
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Windows Image Acquisition (WIA)
'             https://msdn.microsoft.com/en-us/library/windows/desktop/ms630368(v=vs.85).aspx
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sInitialImage : Fully qualified path and filename of the original image to resize
' sOutputImage  : Fully qualified path and filename of where to save the new image
'
' Usage:
' ~~~~~~
' Call WIA_FlipHorizontalAndVerticalImage("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", _
'                                         "C:\Users\MyUser\Desktop\Chrysanthemum_small.jpg")
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2019-06-04              Initial Blog Release
'---------------------------------------------------------------------------------------
Public Function WIA_FlipHorizontalAndVerticalImage(sInitialImage As String, _
                                                   sOutputImage As String) As Boolean
    On Error GoTo Error_Handler
    Dim oWIA                  As Object    'WIA.ImageFile
    Dim oIP                   As Object    'ImageProcess
 
    'Should check if the output file already exists and if so,
    'prompt the user to overwrite it or not
    
    Set oWIA = CreateObject("WIA.ImageFile")
    Set oIP = CreateObject("WIA.ImageProcess")
 
    oIP.Filters.Add oIP.FilterInfos("RotateFlip").FilterID
    oIP.Filters(1).Properties("FlipHorizontal") = True
    oIP.Filters(1).Properties("FlipVertical") = True
 
    oWIA.LoadFile sInitialImage
    Set oWIA = oIP.Apply(oWIA)
    oWIA.SaveFile sOutputImage
    WIA_FlipHorizontalAndVerticalImage = True
 
Error_Handler_Exit:
    On Error Resume Next
    If Not oIP Is Nothing Then Set oIP = Nothing
    If Not oWIA Is Nothing Then Set oWIA = Nothing
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: WIA_FlipHorizontalAndVerticalImage" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

 

A Function To Do It All!

Instead of needing to have multiple functions to handle different flipping needs, let’s build a simple replacement for them all that allows us to specify at runtime the flip operation(s) to apply:

  • Horizontal
  • Vertical
  • Both
'---------------------------------------------------------------------------------------
' Procedure : WIA_FlipImage
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Flip an image horizontally and/or vertically using WIA
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Windows Image Acquisition (WIA)
'             https://msdn.microsoft.com/en-us/library/windows/desktop/ms630368(v=vs.85).aspx
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sInitialImage     : Fully qualified path and filename of the original image to resize
' sOutputImage      : Fully qualified path and filename of where to save the new image
' bFlipHorizontally : Should the image be flipped horizontally (True/False)
' bFlipVertically   : Should the image be flipped vertically (True/False)
'
' Usage:
' ~~~~~~
' Flip Horizontally
' Call WIA_FlipImage("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", _
'                    "C:\Users\MyUser\Desktop\Chrysanthemum_small.jpg", True)
'
' Flip Vertically
' Call WIA_FlipImage("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", _
'                    "C:\Users\MyUser\Desktop\Chrysanthemum_small.jpg", , True)
'
' Flip Horizontally & Vertically
' Call WIA_FlipImage("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", _
'                    "C:\Users\MyUser\Desktop\Chrysanthemum_small.jpg", True, True)
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2019-06-04              Initial Blog Release
' 2         2024-03-01              Changed function name
'---------------------------------------------------------------------------------------
Public Function WIA_FlipImage(sInitialImage As String, _
                              sOutputImage As String, _
                              Optional bFlipHorizontally As Boolean, _
                              Optional bFlipVertically As Boolean) As Boolean
    On Error GoTo Error_Handler
    Dim oWIA                  As Object    'WIA.ImageFile
    Dim oIP                   As Object    'ImageProcess
 
    'Should check if the output file already exists and if so,
    'prompt the user to overwrite it or not
    
    Set oWIA = CreateObject("WIA.ImageFile")
    Set oIP = CreateObject("WIA.ImageProcess")
 
    oIP.Filters.Add oIP.FilterInfos("RotateFlip").FilterID
    If bFlipHorizontally Then oIP.Filters(1).Properties("FlipHorizontal") = True
    If bFlipVertically Then oIP.Filters(1).Properties("FlipVertical") = True
 
    oWIA.LoadFile sInitialImage
    Set oWIA = oIP.Apply(oWIA)
    oWIA.SaveFile sOutputImage
    WIA_FlipImage = True
 
Error_Handler_Exit:
    On Error Resume Next
    If Not oIP Is Nothing Then Set oIP = Nothing
    If Not oWIA Is Nothing Then Set oWIA = Nothing
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: WIA_FlipImage" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

Now, we have a function that does it all.
 

One response on “VBA – WIA – Flip an Image

  1. Les

    I used your code today for rotating an image and it worked PERFECTLY.
    I used the following code to replace the original picture with the rotated picture.
    Private Sub cmdRotate_Click()
    Dim inputPicFile, outputPicFile As String

    inputPicFile = rPictureFile & “.jpg”
    outputPicFile = rPictureFile & “A.jpg”
    Call WIA_RotateImage(inputPicFile, outputPicFile, 90)
    Kill inputPicFile
    Name outputPicFile As inputPicFile
    frameShowPicture.Picture = LoadPicture(inputPicFile)
    End Sub
    Thank you very much for your code.