locked
pictures change height width RRS feed

  • Question

  • is it possible in access to loop through a directory whit pictures, and change the dimensions of the picture ?
    Wednesday, January 18, 2017 2:41 PM

Answers

  • No, not natively.

    That said, there are you can use COM Ojects or automate other programs to do it.  For instance, I created some very simple code to automate IrfanView to do this for a client.

    I seen people automated Image Magick
    Stephen Lebans had an image class that apparently could do this type of thing and much more http://www.lebans.com/imageclass.htm
    FreeImage -> http://freeimage.sourceforge.net
    Windows Image Acquisition -> see: https://www.experts-exchange.com/questions/26921988/Resize-Pictures-in-Access.html for an example (last post)


    Daniel Pineault, 2010-2016 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Wednesday, January 18, 2017 2:53 PM
  • Here, I put together the following using WIA.

    '---------------------------------------------------------------------------------------
    ' Procedure : WIA_ResizeImage
    ' Author    : Daniel Pineault, CARDA Consultants Inc.
    ' Website   : http://www.cardaconsultants.com
    ' Purpose   : Resize an image based on Max width and Max height using WIA
    ' Copyright : The following may be altered and reused as you wish so long as the
    '             copyright notice is left unchanged (including Author, Website and
    '             Copyright).  It may not be sold/resold or reposted on other sites (links
    '             back to this site are allowed).
    ' 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
    ' sResizedImage : Fully qualified path and filename of where to save the resized image
    ' lMaximumWidth : Maximum allowable image width
    ' lMaximumHeight: Maximum allowable image height
    '
    ' Usage:
    ' ~~~~~~
    ' Call WIA_ResizeImage("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", _
    '                      "C:\Users\MyUser\Desktop\Chrysanthemum_small.jpg", _
    '                      800, 600)
    '
    ' Revision History:
    ' Rev       Date(yyyy/mm/dd)        Description
    ' **************************************************************************************
    ' 1         2017-01-18              Initial Release
    '---------------------------------------------------------------------------------------
    Public Function WIA_ResizeImage(sInitialImage As String, sResizedImage As String, _
                                               lMaximumWidth As Long, lMaximumHeight As Long) As Boolean
        On Error GoTo Error_Handler
        Dim oWIA                  As Object 'WIA.ImageFile
        Dim oIP                   As Object 'ImageProcess
    
        Set oWIA = CreateObject("WIA.ImageFile")
        Set oIP = CreateObject("WIA.ImageProcess")
    
        oIP.Filters.Add oIP.FilterInfos("Scale").FilterID
        oIP.Filters(1).Properties("MaximumWidth") = lMaximumWidth
        oIP.Filters(1).Properties("MaximumHeight") = lMaximumHeight
    
        oWIA.LoadFile sInitialImage
        Set oWIA = oIP.Apply(oWIA)
        oWIA.SaveFile sResizedImage
        WIA_ResizeImage = 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 occured" & vbCrLf & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Source: WIA_ResizeImage" & vbCrLf & _
               "Error Description: " & Err.Description & _
               Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
               , vbOKOnly + vbCritical, "An Error has Occured!"
        Resume Error_Handler_Exit
    End Function

    So you could use the function found at http://www.devhut.net/2012/07/13/vba-list-of-files-in-a-specified-folder-or-directory/ and then call the above function to resize each image file in the loop.


    Daniel Pineault, 2010-2016 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com  
    MS Access Tips and Code Samples: http://www.devhut.net




    Wednesday, January 18, 2017 6:12 PM
  • Since this discussion I've create 2 posts on my blog that may be useful to anyone looking into this type of thing.  Please refer to

    VBA – Resize Image

    VBA – Manipulate Images, Resize, Convert and More …


    Daniel Pineault, 2010-2016 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Monday, January 23, 2017 7:07 PM

All replies

  • No, not natively.

    That said, there are you can use COM Ojects or automate other programs to do it.  For instance, I created some very simple code to automate IrfanView to do this for a client.

    I seen people automated Image Magick
    Stephen Lebans had an image class that apparently could do this type of thing and much more http://www.lebans.com/imageclass.htm
    FreeImage -> http://freeimage.sourceforge.net
    Windows Image Acquisition -> see: https://www.experts-exchange.com/questions/26921988/Resize-Pictures-in-Access.html for an example (last post)


    Daniel Pineault, 2010-2016 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Wednesday, January 18, 2017 2:53 PM
  • Many thanks , i' ve to study these options , but it helps me a lot.
    Wednesday, January 18, 2017 3:41 PM
  • is it possible in access to loop through a directory whit pictures, and change the dimensions of the picture ?

    http://www.irfanview.com/

    It's free for non-commercial use and the feature you're looking for is in File - Batch conversion.

    - Peter

    Wednesday, January 18, 2017 5:23 PM
  • Here, I put together the following using WIA.

    '---------------------------------------------------------------------------------------
    ' Procedure : WIA_ResizeImage
    ' Author    : Daniel Pineault, CARDA Consultants Inc.
    ' Website   : http://www.cardaconsultants.com
    ' Purpose   : Resize an image based on Max width and Max height using WIA
    ' Copyright : The following may be altered and reused as you wish so long as the
    '             copyright notice is left unchanged (including Author, Website and
    '             Copyright).  It may not be sold/resold or reposted on other sites (links
    '             back to this site are allowed).
    ' 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
    ' sResizedImage : Fully qualified path and filename of where to save the resized image
    ' lMaximumWidth : Maximum allowable image width
    ' lMaximumHeight: Maximum allowable image height
    '
    ' Usage:
    ' ~~~~~~
    ' Call WIA_ResizeImage("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", _
    '                      "C:\Users\MyUser\Desktop\Chrysanthemum_small.jpg", _
    '                      800, 600)
    '
    ' Revision History:
    ' Rev       Date(yyyy/mm/dd)        Description
    ' **************************************************************************************
    ' 1         2017-01-18              Initial Release
    '---------------------------------------------------------------------------------------
    Public Function WIA_ResizeImage(sInitialImage As String, sResizedImage As String, _
                                               lMaximumWidth As Long, lMaximumHeight As Long) As Boolean
        On Error GoTo Error_Handler
        Dim oWIA                  As Object 'WIA.ImageFile
        Dim oIP                   As Object 'ImageProcess
    
        Set oWIA = CreateObject("WIA.ImageFile")
        Set oIP = CreateObject("WIA.ImageProcess")
    
        oIP.Filters.Add oIP.FilterInfos("Scale").FilterID
        oIP.Filters(1).Properties("MaximumWidth") = lMaximumWidth
        oIP.Filters(1).Properties("MaximumHeight") = lMaximumHeight
    
        oWIA.LoadFile sInitialImage
        Set oWIA = oIP.Apply(oWIA)
        oWIA.SaveFile sResizedImage
        WIA_ResizeImage = 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 occured" & vbCrLf & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Source: WIA_ResizeImage" & vbCrLf & _
               "Error Description: " & Err.Description & _
               Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
               , vbOKOnly + vbCritical, "An Error has Occured!"
        Resume Error_Handler_Exit
    End Function

    So you could use the function found at http://www.devhut.net/2012/07/13/vba-list-of-files-in-a-specified-folder-or-directory/ and then call the above function to resize each image file in the loop.


    Daniel Pineault, 2010-2016 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com  
    MS Access Tips and Code Samples: http://www.devhut.net




    Wednesday, January 18, 2017 6:12 PM
  • Since this discussion I've create 2 posts on my blog that may be useful to anyone looking into this type of thing.  Please refer to

    VBA – Resize Image

    VBA – Manipulate Images, Resize, Convert and More …


    Daniel Pineault, 2010-2016 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Monday, January 23, 2017 7:07 PM
  • Mr. Pineault, this is really awesome , many many thanks !

    Tuesday, January 31, 2017 9:19 AM
  • No problem.  It was actually fun to fiddle with!

    Daniel Pineault, 2010-2016 Microsoft MVP Professional Support: http://www.cardaconsultants.com MS Access Tips and Code Samples: http://www.devhut.net

    Tuesday, January 31, 2017 1:27 PM
  • Hello , mister Pineault,

    I don't know if this does exist , but is there a way to crop images ?

    Saturday, April 29, 2017 5:37 AM