Resize image 50% small Ms Access Gurus

VBA > Image > Resize Image Irfanview

Rather than putting images in Access, it is better to store them as external files. In addition to being more efficient, then you can also use tools like Irfanview to resize them. Irfanview is a freeware image viewer and editor. Access can use Shell to run Irfanview in the background with command line switches (ioptions) to manipulate images and more.

Access can also use the Windows Image Acquisition (WIA) library. See code using WIA by Geoff Griffith.

Screen shot

This is more for quick understanding than reality ... and also closes something for me Resize image 50%

Examples — Switches you can end to the end of the command line for Irfanview

Logic

Even though VBA is controlling everything (you can use Access, Excel, Word, PowerPoint, or other Office application), first, install Irfanview so you can use its features. Watch each dialog box of the installation and write down the path and filename of the executable, when it is displayed, so you can put it into the VBA code (you can also get the path\file when it is done if you know how to find it). My path\file is

In the code, customize sPathFileIrfanview, sPathFileImageSource, and sPathFileImageTarget for your path\filenames.

Look up the switches you want and customize sCommand.

Shell runs an external program. The command specifies the program to run (Irfanview), the file to open (the image you want to change), the switches to use (what you want to do), and saves the converted file with a new name.

If the Shell command was successful, a message box using MsgBox appears, and then the folder with the converted file opens using FollowHyperlink.

If Shell was not successful, the user gets a message box.

If Shell returns zero (0), it wasn't successful. Otherwise, the function returns the TaskID. Thanks for this suggestion, Geoff Griffith, but it appears that shell to Irfanview doesn't return zero for a problem.

Compile and save the code, before and after you customize it. When prompted for the module name, if you don't have your own name, use this so the module name is logical and not the same as any procedure name:

You can use this logic in a loop that looks at all the files in a folder, or just files of a specific type. For VBA code to loop through files, see VBA > File > Loop and Rename

Irfanview appear to create a path if it can, and you specify something that doesn't exist. Otherwise, here is code to Make a Path

Parameters

There aren't any parameters, but you would want to modify this and add them, to replace the custom information, once you test it and see how it works. You'd probably also want to make the switches a passed parameter. And customize, or comment, the message box at the end, and comment opening the folder if you are looping.

Return

Return result of Shell (double even though it appears to be a long integer).

Code

' Module Name: mod_Irfanview_ResizeImage
'*************** Code Start *****************************************************
' Purpose  : Use Irfanview to resize an image file and save it with a different name
' Author   : crystal (strive4peace)
' Return   : Double (result of Shell)
' License  : below code
' Code List: www.MsAccessGurus.com/code.htm
'--------------------------------------------------------------------------------

' Irfanview_ResizeImage

'--------------------------------------------------------------------------------' Public Function Irfanview_ResizeImage() As Double '161222 strive4peace, 190125...190204 ' download Irfanview, freeware image editor http://www.irfanview.com 'if there is an error, continue On Error GoTo Proc_Err 'initialize return value to be 0 for not successful Irfanview_ResizeImage = 0 Dim sCommand As String _ , sPathFileIrfanview As String _ , sPathFileImageSource As String _ , sPathFileImageTarget As String _ , sMsg As String ' Switch examples: ' /aspectratio keep image proportions when resizing ' /resize=(w,h) resize to w (width) and h (height) ' /resize_long=X resize : set long side to X ' /resize_short=X resize : set short side to X ' ----------------------------------------- customize sPathFileIrfanview = "C:\Program Files (x86)\IrfanView\i_view32.exe" sPathFileImageSource = "c:\path\IMG_8909.JPG" sPathFileImageTarget = "c:\path\IMG_8909_516.JPG" ' ----------------------------------------- 'specify Command for Shell: ' path and filename for Irfanview executable, ' path and filename of image to convert, ' switches: ' resize so longest side is 516 pixels, keep aspect ratio, ' and save converted file in another location instead of over-writing sCommand = sPathFileIrfanview _ & " " & sPathFileImageSource _ & " /resize_long=516" _ & " /aspectratio" _ & " /convert=" & sPathFileImageTarget ' ----customize 'Shell to Irfanview and hide process Irfanview_ResizeImage = Shell(sCommand, vbHide) 'If Irfanview_ResizeImage <> 0 Then 'success 'message to user sMsg="Created " & sPathFileImageTarget MsgBox sMsg,,"Done" 'open path Application.FollowHyperlink Left(sPathFileImageTarget, InStrRev(sPathFileImageTarget, "\")) 'Else 'Error message to user 'sMsg="Error creating " & sPathFileImageTarget 'MsgBox sMsg,,"Error" 'End if Proc_Exit: On Error Resume Next Exit Function Proc_Err: MsgBox Err.Description _ , , "ERROR " & Err.Number _ & " Irfanview_ResizeImage" Resume Proc_Exit Resume End Function ' ' LICENSE ' You may freely use and share this code ' provided this license notice and comment lines are not changed; ' code may be modified provided you clearly note your changes. ' You may not sell this code alone, or as part of a collection, ' without my handwritten permission. ' All ownership rights reserved. Use at your own risk. ' ~ crystal (strive4peace) www.MsAccessGurus.com '*************** Code End *******************************************************

Goto Top  

Back Story

Since I love music so much, and to continue with posting a snippet each day for Advent, for Christmas, I planned to post code to expand what I've already posted for playing sounds — but in the 12 days of Christmas, I couldn't get it to do what I hoped, and got caught up trying to figure out how to overlay sounds, and looping gave me a loop ... still working on it ... 'not possible' is not in my understanding. Better yet would be to specify waveform parameters to play at a particular pitch, duration, attack, and decay. I don't think VBA can do that though! ... but maybe files can be defined for other services that can ... still exploring.

So here we are, with another love of mine – pictures. Recently, someone emailed me and asked about adjusting a bunch of pictures to be the same size for a member directory that they are using Access to print. You asked me this at the right time, Jan.

References

Docs / Office VBA Reference / Language reference / Reference / Functions / Shell

Help: Shell function

Docs / Office VBA Reference / Language reference / Reference / Functions / MsgBox

Help: MsgBox function

Docs / Office VBA Reference / Access / Object model / Application object / Methods / FollowHyperlink

Help: Application.FollowHyperlink method (Access)

References - IrfanView

IrfanView Support Index

IrfanView Support Index

IrfanView Support Forum

IrfanView Support Forum

Irfanview Frequently Asked Questions (FAQ)

Frequently Asked Questions about IrfanView

Irfanview Command Line Switches

Irfanview Command line and scripts

Goto Top  

Share with others

here's the link to copy:

https://MsAccessGurus.com/VBA/Code/Irfanview_ResizeImage.htm

Do you have something to say or share?

It is interesting to hear from you. Was something not clear? Did you find a bug? Is an explanation wrong or not sufficient? Do you want the code do more (there is always more)?

Some of you write to say thanks and tell me what you're doing with Access ... its nice to get an echo back. I want you and others to be good with Access, and other Office applications like Excel, Word, and PowerPoint ... and Windows. Take advantage of the strengths in each to manage your information wisely.

Are you a developer? Do you want to share? Email to ask about getting your pages added to the code index.

When we communicate, collaborate, and appreciate, we all get better. Thank you. Email me at info@msAccessGurus.com

Goto Top