Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit picture size to 1mb or less

Status
Not open for further replies.

oxicottin

Programmer
Jun 20, 2008
353
US
Hello, First I want to say this code is not mine I have found it years ago and been using it since in a Visual Work Instruction database since to get images for each instruction or step in a process. Anyway, I have a button on my form that has the code below:

Code:
Private Sub cmdImg_Add_Click()
On Error GoTo err_cmdImg_Add_Click

   Dim strDialogTitle As String
   Dim PathStrg As String
   Dim msg As String
   Dim relativePath As String
   Dim dbPath As String
   
If Not IsNull(txtJobStepID) Then

        'strDialogTitle = "Select an image
          PathStrg = GetOpenFile_CLT(".\", strDialogTitle)
       
        'If no file was selected then the PathStrg variable will be empty.
        'If there was a file selected then.....
          If PathStrg <> "" Then
   
        'setup new file name and appropriate DB subfolder
          relativePath = "\VWI_Images\" & Me.txtJobStepID & ".jpg"
          'Finds BE path in module-modGetPath
          dbPath = GetCurrentPath()
        
        'copy selected file with new name and subfolder
          FileCopy LCase(PathStrg), dbPath & relativePath
        
        'update the table field with the new file name and relative location
          Me!ImagePath.Value = relativePath
        'display the image from the subfolder of the DB
          Me.Requery
            
   End If
   
Else
         'If no ID number in (txtJobStepID) then display message box
          MsgBox "You must enter a job step before adding an image.", vbExclamation, "Enter A Job Step"

exit_cmdImg_Add_Click:
    Exit Sub
    
err_cmdImg_Add_Click:
    Select Case Err.Number
        Case 70
            msg = "You are already using this image already for another step or VWI" & Chr(10) & "Image was not replaced."
            MsgBox msg, vbOKOnly + vbInformation, "Add/Change Image", Err.HelpFile, Err.HelpContext
        Case Else
            msg = "Error # " & Str(Err.Number) & Chr(13) & Err.Description
            MsgBox msg, vbOKOnly, "Add/Change Image Button Error", Err.HelpFile, Err.HelpContext
        End Select
    
    Resume exit_cmdImg_Add_Click
    
 End If

End Sub

And it opens a window for me to browse my PC for a image I want to use/upload to the database folder ect. and it has always worked great. The problem is I have always had to resize my images to keep the DB folder size down and once in a while someone will put an image in thats like 8megs and I have to go through and resize it. What I wnat to do is if the image exceeds 1mb then a message box comes up telling the user to resize the image and it automatically opens Microsoft Office Picture Manager and closes the one window. How can I do this? below is the module it uses as well.

Code:
Option Compare Database
Option Explicit

' Code for this module "basOpenFile" Wus taken from a sample DB written by Agnieszka Wiklendt


' Declarations for Windows Common Dialogs procedures
Private Type CLTAPI_OPENFILE
  strFilter As String             ' Filter string
  intFilterIndex As Long          ' Initial Filter to display.
  strInitialDir As String         ' Initial directory for the dialog to open in.
  strInitialFile As String        ' Initial file name to populate the dialog with.
  strDialogTitle As String        ' Dialog title
  strDefaultExtension As String   ' Default extension to append to file if user didn't specify one.
  lngFlags As Long                ' Flags (see constant list) to be used.
  strFullPathReturned As String   ' Full path of file picked.
  strFileNameReturned As String   ' File name of file picked.
  intFileOffset As Integer        ' Offset in full path (strFullPathReturned) where the file name (strFileNameReturned) begins.
  intFileExtension As Integer     ' Offset in full path (strFullPathReturned) where the file extension begins.
End Type

Const ALLFILES = "All Files"

Private Type CLTAPI_WINOPENFILENAME
    lStructSize As Long
    hWndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustrFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustrData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Const OFN_ALLOWMULTISELECT = &H200
Const OFN_CREATEPROMPT = &H2000
Const OFN_EXPLORER = &H80000
Const OFN_FILEMUSTEXIST = &H1000
Const OFN_HIDEREADONLY = &H4
Const OFN_NOCHANGEDIR = &H8
Const OFN_NODEREFERENCELINKS = &H100000
Const OFN_NONETWORKBUTTON = &H20000
Const OFN_NOREADONLYRETURN = &H8000
Const OFN_NOVALIDATE = &H100
Const OFN_OVERWRITEPROMPT = &H2
Const OFN_PATHMUSTEXIST = &H800
Const OFN_READONLY = &H1
Const OFN_SHOWHELP = &H10

Declare Function CLTAPI_GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
  (pOpenfilename As CLTAPI_WINOPENFILENAME) _
As Boolean
  
Declare Function CLTAPI_GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
  (pOpenfilename As CLTAPI_WINOPENFILENAME) _
As Boolean
  
Declare Sub CLTAPI_ChooseColor Lib "msaccess.exe" Alias "#53" _
  (ByVal hwnd As Long, rgb As Long)

Function GetOpenFile_CLT(strInitialDir As String, strTitle As String) As String
  ' Comments  : Simple file open routine. For additional options, use GetFileOpenEX_CLT()
  ' Parameters: strInitialDir - path for the initial directory, or blank for the current directory
  '             strTitle - title for the dialog
  ' Returns   : string path, name and extension of the file selected
  '
  Dim fOK As Boolean
  Dim typWinOpen As CLTAPI_WINOPENFILENAME
  Dim typOpenFile As CLTAPI_OPENFILE
  Dim strFilter As String
  
  On Error GoTo PROC_ERR
  
  ' Set reasonable defaults for the structure
  
  strFilter = CreateFilterString_CLT("JPEG files (*.JPG)", "*.JPG", "GIF image files (*.GIF)", "*.GIF", "Bitmap files (*.BMP)", "*.BMP")
  ' strFilter = CreateFilterString_CLT("JPEG image files (*.JPG)", "*.JPG", "GIF image files (*.GIF)", "*.GIF")
  '(original in the above line)
  ' to add more, use same format separated by commas within the CLT()
  ' e.g.: All Files (*.*)", "*.*",
  
  If strInitialDir <> "" Then
    typOpenFile.strInitialDir = strInitialDir
  Else
    typOpenFile.strInitialDir = CurDir()
  End If
  
  If strTitle <> "" Then
    typOpenFile.strDialogTitle = strTitle
  End If
  
  typOpenFile.strFilter = strFilter
  typOpenFile.lngFlags = OFN_HIDEREADONLY Or OFN_SHOWHELP
  
  ' Convert the CLT structure to a Win structure
  ConvertCLT2Win typOpenFile, typWinOpen
  
  ' Call the Common dialog
  fOK = CLTAPI_GetOpenFileName(typWinOpen)
  
  ' Convert the Win structure back to a CLT structure
  ConvertWin2CLT typWinOpen, typOpenFile
  
  GetOpenFile_CLT = typOpenFile.strFullPathReturned
      
PROC_EXIT:
  Exit Function
  
PROC_ERR:
  GetOpenFile_CLT = ""
  Resume PROC_EXIT

End Function

Sub ConvertCLT2Win(CLT_Struct As CLTAPI_OPENFILE, Win_Struct As CLTAPI_WINOPENFILENAME)
  ' Comments  : Converts the passed CLTAPI structure to a Windows structure
  ' Parameters: CLT_Struct - record of type CLTAPI_OPENFILE
  '             Win_Struct - record of type CLTAPI_WINOPENFILENAME
  ' Returns   : Nothing
  '
  Dim strFile As String * 512

  On Error GoTo PROC_ERR
  
  Win_Struct.hWndOwner = Application.hWndAccessApp
  Win_Struct.hInstance = 0

  If CLT_Struct.strFilter = "" Then
    Win_Struct.lpstrFilter = ALLFILES & Chr$(0) & "*.*" & Chr$(0)
  Else
    Win_Struct.lpstrFilter = CLT_Struct.strFilter
  End If
  Win_Struct.nFilterIndex = CLT_Struct.intFilterIndex

  Win_Struct.lpstrFile = String(512, 0)
  Win_Struct.nMaxFile = 511
  
  Win_Struct.lpstrFileTitle = String$(512, 0)
  Win_Struct.nMaxFileTitle = 511

  Win_Struct.lpstrTitle = CLT_Struct.strDialogTitle
  Win_Struct.lpstrInitialDir = CLT_Struct.strInitialDir
  Win_Struct.lpstrDefExt = CLT_Struct.strDefaultExtension

  Win_Struct.Flags = CLT_Struct.lngFlags

  Win_Struct.lStructSize = Len(Win_Struct)
  
PROC_EXIT:
  Exit Sub
  
PROC_ERR:
  Resume PROC_EXIT
   
End Sub

Sub ConvertWin2CLT(Win_Struct As CLTAPI_WINOPENFILENAME, CLT_Struct As CLTAPI_OPENFILE)
  ' Comments  : Converts the passed CLTAPI structure to a Windows structure
  ' Parameters: Win_Struct - record of type CLTAPI_WINOPENFILENAME
  '             CLT_Struct - record of type CLTAPI_OPENFILE
  ' Returns   : Nothing
  '
  On Error GoTo PROC_ERR
      
  CLT_Struct.strFullPathReturned = Left(Win_Struct.lpstrFile, InStr(Win_Struct.lpstrFile, vbNullChar) - 1)
  CLT_Struct.strFileNameReturned = RemoveNulls_CLT(Win_Struct.lpstrFileTitle)
  CLT_Struct.intFileOffset = Win_Struct.nFileOffset
  CLT_Struct.intFileExtension = Win_Struct.nFileExtension
  
PROC_EXIT:
  Exit Sub
  
PROC_ERR:
  Resume PROC_EXIT
  
End Sub

Function CreateFilterString_CLT(ParamArray varFilt() As Variant) As String
  ' Comments  : Builds a Windows formatted filter string for "file type"
  ' Parameters: varFilter - parameter array in the format:
  '                          Text, Filter, Text, Filter ...
  '                         Such as:
  '                          "All Files (*.*)", "*.*", "Text Files (*.TXT)", "*.TXT"
  ' Returns   : windows formatted filter string
  '
  Dim strFilter As String
  Dim intCounter As Integer
  Dim intParamCount As Integer

  On Error GoTo PROC_ERR
  
  ' Get the count of paramaters passed to the function
  intParamCount = UBound(varFilt)
  
  If (intParamCount <> -1) Then
    
    ' Count through each parameter
    For intCounter = 0 To intParamCount
      strFilter = strFilter & varFilt(intCounter) & Chr$(0)
    Next
    
    ' Check for an even number of parameters
    If (intParamCount Mod 2) = 0 Then
      strFilter = strFilter & "*.*" & Chr$(0)
    End If
    
  End If

  CreateFilterString_CLT = strFilter
  
PROC_EXIT:
  Exit Function
  
PROC_ERR:
  CreateFilterString_CLT = ""
  Resume PROC_EXIT
  
End Function

Function RemoveNulls_CLT(strIn As String) As String
  ' Comments  : Removes terminator from a string
  ' Parameters: strIn - string to modify
  ' Return    : modified string
  '
  Dim intChr As Integer

  intChr = InStr(strIn, Chr$(0))

  If intChr > 0 Then
    RemoveNulls_CLT = Left$(strIn, intChr - 1)
  Else
    RemoveNulls_CLT = strIn
  End If

End Function

Thanks,
SoggyCashew.....
 
Thank You 1DMF I will look into his suggestion...

Thanks,
SoggyCashew.....
 
A bunch of years ago I came across janGraphics.DLL, a freeware app library that I Set a reference to, and a little code will take a 3 - 4 meg JPG and shrink it down to less than 100K (My application does not need high resolution) but the shrinkage can be set with code. I just googled it, and I don't see much to help, but after setting a reference my code is simple:

Set conv = CreateObject("jangraphics.compendium")
J = conv.convertEx(SourceFile, DestinationFile, 1000, 1000, False, 23)

I has worked for me from Access 2003 - 2013. I think you can download it, if not I will post it.

Thanks,
ChaZ




There Are 10 Types Of People In The world:
Those That Understand BINARY And Those That DonÆt.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top