[ .NET, WinForms, Graphics ]
Image resizing is a common task in software development, especially when dealing with photos and other visual media. It can be time-consuming and cumbersome to resize a large number of images manually, which is where automated tools come in handy. In this blog post, we’ll explore an application written in VB.NET that allows the bulk resizing of images to a given size.
The application has a simple and intuitive user interface, making it easy for users to select multiple images and specify the maximum size they want the images to be. Once the user has selected the images and entered the desired size, the application resizes the images and saves them to the same directory as the original images with a “-wp” suffix added to the file name.
The code for the application uses the .NET framework’s Graphics class to resize the images. It first creates a new Bitmap object from the source image and then calculates the new width and height based on the desired size. It then creates a new Bitmap object with the new width and height and uses the Graphics class to draw the resized image onto the new Bitmap. The application uses high-quality bilinear interpolation to ensure that the resized images look as good as possible.
One important feature of the application is its ability to handle large images without consuming too much memory. It achieves this by processing the images in chunks rather than loading the entire image into memory at once. This helps prevent the application from crashing when processing large images.
Here’s the main operating block that actually does the job:
Private Sub ResizeImage(ByVal sourceFilePath As String, ByVal maxSize As Integer)
' Load the source image
Dim sourceImage As New Bitmap(sourceFilePath)
' Determine the new dimensions of the image
Dim widthRatio As Double = sourceImage.Width / maxSize
Dim heightRatio As Double = sourceImage.Height / maxSize
Dim scaleFactor As Double = Math.Max(widthRatio, heightRatio)
Dim newWidth As Integer = CInt(Math.Round(sourceImage.Width / scaleFactor))
Dim newHeight As Integer = CInt(Math.Round(sourceImage.Height / scaleFactor))
' Create a new bitmap with the new dimensions
Dim newImage As New Bitmap(newWidth, newHeight)
' Resize the image
Using g As Graphics = Graphics.FromImage(newImage)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
g.DrawImage(sourceImage, 0, 0, newWidth, newHeight)
End Using
' Save the resized image
Dim extension As String = Path.GetExtension(sourceFilePath)
Dim newFilePath As String = Path.Combine(Path.GetDirectoryName(sourceFilePath),
Path.GetFileNameWithoutExtension(sourceFilePath) & "-resized" & extension)
newImage.Save(newFilePath, sourceImage.RawFormat)
End Sub
- Loads the source image into a Bitmap object using the path of the image file.
- Determines the new dimensions of the image by calculating the ratios of the source image’s width and height to the maximum size, and then chooses the larger ratio as the scale factor. The new dimensions are then calculated by dividing the source image’s dimensions by the scale factor.
- Creates a new Bitmap object with the calculated new dimensions.
- Resizes the source image to fit the new dimensions of the new Bitmap object using the Graphics object of the new Bitmap object. The interpolation mode of the Graphics object is set to “HighQualityBilinear” to improve the quality of the resized image.
- Saves the resized image as a new file by constructing a new file path using the directory, name, and extension of the original file. The file is saved in the same format as the source image file.
Overall, this application is a great tool for anyone who needs to resize a large number of images quickly and easily. It’s a testament to the power and versatility of the .NET framework and demonstrates how simple it can be to create useful applications with just a few lines of code.
Download: PhotosToWeb
Leave a reply