|
|
Title | Resize all of the graphic files in a directory in Visual Basic 6 |
Description | This example shows how to resize all of the graphic files in a directory in Visual Basic . |
Keywords | graphics, resize picture, resize image, directory, Visual Basic 6 |
Categories | Graphics, Algorithms, Files and Directories |
|
|
This program lets you enter a directory name and a scale factor. It then loads all of the bitmap, GIF, and JPEG files in that directory, resizes them, and saves the resized versions back into the directory.
The most interesting work occurs when you click the Go button. The code first gets the scale factor and sets the form's cursor to a wait cursor.
Next the program uses the Dir$ function to loop through the files in the directory that you entered. It gets the file's extension and, if the extension is .bmp, .gif, .jpg, or .jpeg (you can add others if you like), it processes that file.
The code loads the file into the hidden PictureBox picOriginal. It copies the picture into the visible PictureBox picVisible so you can see what's happening. It then size the hidden PictureBox picResized to match the scale you entered and copies the original picture into it.
Finally the program composes a new file name and saves the resized image in a file.
|
|
' Process the pictures.
Private Sub cmdGo_Click()
Dim dir_name As String
Dim file_name As String
Dim ext As String
Dim pos As Integer
Dim the_scale As Single
the_scale = Val(txtScale.Text)
If the_scale <= 0 Then
MsgBox "Scale must be greater than zero.", _
vbOKOnly, "Invalid Scale"
Exit Sub
End If
On Error GoTo UnknownError
Me.MousePointer = vbHourglass
DoEvents
dir_name = txtDirectory.Text
If Right$(dir_name, 1) <> "\" Then dir_name = dir_name _
& "\"
file_name = Dir$(dir_name & "*.*")
Do While Len(file_name) > 0
On Error GoTo FileError
' Get the file's extension.
pos = InStrRev(file_name, ".")
If pos = 0 Then
ext = ""
Else
ext = LCase$(Mid$(file_name, pos))
End If
' See if it's a graphic file.
Select Case ext
Case ".bmp", ".gif", ".jpg", ".jpeg"
' Load and display the image.
Me.Caption = "howto_resize_pics - " & _
file_name
picOriginal.Picture = LoadPicture(dir_name _
& file_name)
picVisible.PaintPicture _
picOriginal.Picture, _
0, 0, picVisible.Width, _
picVisible.Height, _
0, 0, picOriginal.Width, _
picOriginal.Height
DoEvents
' Resize the image.
picResized.Cls
picResized.Width = picOriginal.Width * _
the_scale
picResized.Height = picOriginal.Height * _
the_scale
picResized.PaintPicture _
picOriginal.Picture, _
0, 0, picResized.Width, _
picResized.Height, _
0, 0, picOriginal.Width, _
picOriginal.Height
' Save the resized image.
file_name = Left$(file_name, Len(file_name) _
- Len(ext)) & _
"s.bmp"
SavePicture picResized.Image, dir_name & _
file_name
End Select
' Get the next file.
GetNextFile:
file_name = Dir$()
Loop
picVisible.Picture = Nothing
Me.MousePointer = vbDefault
Me.Caption = "howto_resize_pics"
Exit Sub
FileError:
If MsgBox(Err.Description & vbCrLf & "Continue?", _
vbYesNo, "Error") = vbYes Then
Resume GetNextFile
Else
picVisible.Picture = Nothing
Me.MousePointer = vbDefault
Me.Caption = "howto_resize_pics"
Exit Sub
End If
UnknownError:
MsgBox Err.Description, vbOKOnly, "Error"
picVisible.Picture = Nothing
Me.MousePointer = vbDefault
Me.Caption = "howto_resize_pics"
Exit Sub
End Sub
|
|
Unfortunately Visual Basic 6 only knows how to save bitmap files so the scaled files are bitmaps. This reduces one of the main benefits of this program: to make new versions of the files that take less space. (Visual Basic .NET can save images in .gif, .jpeg, and other formats.) At least this version makes images that are small enough to view reasonably.
You can use third party libraries to save the files in other formats.
|
|
|
|
|
|