Pada saat kita menggambil sebuah foto atau gambar hasil jepretan kamera ataupun hp biasanya file gambar mempunyai ukurannya lumayan besar kisaran satuan MB. Lalu pada saat kita membutuhkan file gambar tersebut untuk di Upload pada sebuah aplikasi tertentu timbul pesan “File Gambar terlalu besar”. Misalnya pada saat kita mendaftarkan sebuah aplikasi yang memerlukan verifikasi foto atau gambar, terkadang file yang dibutuhkan sudah di tentukan oleh programmer yang tidak boleh melebihi dari kapasitas yang telah ditentukan.
Dari kasus diatas maka untuk mengatasi masalah tersebut satu-satunya jalan adalah mengkompress image atau gambar yang lebih kecil lagi dari ukuran semula agar bisa di upload sesuai kapasitas file yang ditentukan.
Memang banyak berteberan aplikasi pengkompress file image baik offline maupun online yang bisa kita gunakan namun apa salahnya kalau kita membuatnya sendiri dengan program visual basic 6.0 dengan begitu dapat mempunyai kebanggaan tersendiri.
Private Type BrowseInfo
hwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Private Const BIF_BROWSEFORCOMPUTER = &H1000
Private Const BIF_BROWSEFORPRINTER = &H2000
Private Const BIF_BROWSEINCLUDEFILES = &H4000
Private Const BIF_DONTGOBELOWDOMAIN = &H2
Private Const BIF_EDITBOX = &H10
Private Const BIF_NEWDIALOGSTYLE = &H40
Private Const BIF_RETURNFSANCESTORS = &H8
Private Const BIF_RETURNONLYFSDIRS = &H1
Private Const BIF_STATUSTEXT = &H4
Private Const BIF_USENEWUI = (BIF_NEWDIALOGSTYLE Or BIF_EDITBOX)
Private Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" _
(ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" _
Alias "lstrcatA" (ByVal lpString1 As String, _
ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" _
(lpBI As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" _
(ByVal pidList As Long, ByVal lpBuffer As String) As Long
Const CSIDL_DESKTOP = &H0
Const CSIDL_PROGRAMS = &H2
Const CSIDL_CONTROLS = &H3
Const CSIDL_PRINTERS = &H4
Const CSIDL_PERSONAL = &H5
Const CSIDL_FAVORITES = &H6
Const CSIDL_STARTUP = &H7
Const CSIDL_RECENT = &H8
Const CSIDL_SENDTO = &H9
Const CSIDL_BITBUCKET = &HA
Const CSIDL_STARTMENU = &HB
Const CSIDL_DESKTOPDIRECTORY = &H10
Const CSIDL_DRIVES = &H11
Const CSIDL_NETWORK = &H12
Const CSIDL_NETHOOD = &H13
Const CSIDL_FONTS = &H14
Const CSIDL_TEMPLATES = &H15
Const CSIDL_COMMON_STARTMENU = &H16
Const CSIDL_COMMON_PROGRAMS = &H17
Const CSIDL_COMMON_STARTUP = &H18
Const CSIDL_COMMON_DESKTOPDIRECTORY = &H19
Const CSIDL_APPDATA = &H1A
Const CSIDL_PRINTHOOD = &H1B
Private Type SHITEMID
Cb As Long
AbID As Byte
End Type
Private Type ITEMIDLIST
Mkid As SHITEMID
End Type
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Private Declare Function OleInitialize Lib "ole32.dll" (lp As Any) As Long
Private Declare Sub OleUninitialize Lib "ole32" ()
Private Function fGetSpecialFolder(CSIDL As Long, IDL As ITEMIDLIST) As String
Dim sPath As String
If SHGetSpecialFolderLocation(hwnd, CSIDL, IDL) = 0 Then
sPath = Space$(MAX_PATH)
If SHGetPathFromIDList(ByVal IDL.Mkid.Cb, ByVal sPath) Then
fGetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\"
End If
End If
End Function
Public Function fBrowseForFolder(hwndOwner As Long, sPrompt As String) As String
Dim iNull As Long
Dim lpIDList As Long
Dim lResult As Long
Dim sPath As String
Dim sPath1 As String
Dim udtBI As BrowseInfo
Dim IDL As ITEMIDLIST
'
sPath1 = fGetSpecialFolder(CSIDL_DESKTOP, IDL)
Call OleInitialize(ByVal 0&)
With udtBI
.pIDLRoot = IDL.Mkid.Cb
.hwndOwner = hwndOwner
.lpszTitle = lstrcat(sPrompt, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_USENEWUI
End With
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
lResult = SHGetPathFromIDList(lpIDList, sPath)
Call CoTaskMemFree(lpIDList)
iNull = InStr(sPath, vbNullChar)
If iNull Then sPath = Left$(sPath, iNull - 1)
End If
Call OleUninitialize
fBrowseForFolder = sPath
End Function
Kembali ke Form kemudian ketik kode dibawah ini pada masing-masing komponen Form :
Private Sub cmdCekProperties_Click()
If lblPathLoad.Caption <> vbNullString Then
Call ShowProps(lblPathLoad.Caption, Me.hwnd)
End If
End Sub
Private Sub cmdCompres_Click()
If lblPathTujuan.Caption = vbNullString Then: Exit Sub
Set Picture1 = LoadPicture(lblPathLoad.Caption)
Picture1.ScaleMode = 3
Picture1.AutoRedraw = True
Picture1.PaintPicture Picture1.Picture, _
0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, _
0, 0, Picture1.Picture.Width / 26.46, _
Picture1.Picture.Height / 26.46
Picture1.Picture = Picture1.Image
SavePicture Picture1.Picture, lblPathTujuan.Caption
cmdPropertiesSetelahCompress.Enabled = True
End Sub
Private Sub cmdLoad_Click()
Dim nPath
CommonDialog1.CancelError = True
CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNPathMustExist + cdlOFNFileMustExist
CommonDialog1.Filter = "JPG (*.jpg)|*.jpg|JPEG (*.jpeg)|*.jpeg|BMP (*.bmp)|*.bmp"
CommonDialog1.ShowOpen
nPath = CommonDialog1.FileName
If nPath <> vbNullString Then
lblPathLoad.Caption = nPath
Set Picture1 = LoadPicture(nPath)
Picture1.ScaleMode = 3
Picture1.AutoRedraw = True
Picture1.PaintPicture Picture1.Picture, _
0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, _
0, 0, Picture1.Picture.Width / 26.46, _
Picture1.Picture.Height / 26.46
Picture1.Picture = Picture1.Image
Else
nPath = ""
End If
cmdTujuan.Enabled = True
End Sub
Private Sub cmdPropertiesSetelahCompress_Click()
If lblPathTujuan.Caption <> vbNullString Then
Call ShowProps(lblPathTujuan.Caption, Me.hwnd)
End If
End Sub
Private Sub cmdTujuan_Click()
Dim sStrMySql As String
sStrMySql = fBrowseForFolder(hwnd, "Pilih Direktori Tujuan Simpan")
If sStrMySql <> vbNullString Then
lblPathTujuan.Caption = sStrMySql & "\" & StripPath(lblPathLoad.Caption)
End If
cmdCompres.Enabled = True
End Sub
Private Sub Form_Load()
Set Picture1.Picture = Nothing
lblPathLoad.Caption = ""
lblPathTujuan.Caption = ""
cmdCompres.Enabled = False
cmdTujuan.Enabled = False
cmdPropertiesSetelahCompress.Enabled = False
End Sub
Function StripPath(T$) As String
Dim x%, ct%
StripPath$ = T$
x% = InStr(T$, "\")
Do While x%
ct% = x%
x% = InStr(ct% + 1, T$, "\")
Loop
If ct% > 0 Then StripPath$ = Mid$(T$, ct% + 1)
End Function
Public Sub ShowProps(FileName As String, OwnerhWnd _
As Long)
Dim SEI As SHELLEXECUTEINFO
Dim r As Long
With SEI
.cbSize = Len(SEI)
.fMask = SEE_MASK_NOCLOSEPROCESS Or _
SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
.hwnd = OwnerhWnd
.lpVerb = "properties"
.lpFile = FileName
.lpParameters = vbNullChar
.lpDirectory = vbNullChar
.nShow = 0
.hInstApp = 0
.lpIDList = 0
End With
r = ShellExecuteEX(SEI)
End Sub
Simpan kode Anda dan jalankan program, dan anda siap mengkompress file Image Anda sekarang.
|
Output Program |
apibila ada kesalahan atau error pada program yang Anda buat silahkan tulis dalam komentar.
No comments:
Post a Comment