The .NET framework has the System.Drawing namespace which allows the developer to create and/or manipulate image files programmatically. A common and popular action that a developer might need is to create a thumbnail of an image.
The framework has a built-in method for creating thumbnails, the Image.GetThumbnailImage method. As you can read the method checks the image file if it has a thumbnail image embedded (as meta data) and returns this image. If not, it automatically creates a thumbnail by resizing the original image. This method does the trick but the quality of the thumbnail is low. But we cannot set the quality, so if we want high quality for our thumbnails we need to make our own method.
Our main concern for this method is to be able to have high quality images. There are a few things to consider. Also, the aspect ratio of the image will remain the same. Usually we want our images to be of the same width or height. So providing only e.g. the target width is enough, since we can calculate the target height.
Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Imaging Sub CreateThumb(ByVal SourceImagePath As String, ByVal DestinationImagePath As String, ByVal TargetWidth As Integer) Dim ThumbImage, TemporaryImage As Image TemporaryImage = System.Drawing.Image.FromFile(SourceImagePath)
Firstly, we need to create a bitmap with the desired dimensions. Then we load the source image into a System.Drawing.Rectangle object. Then with a System.Drawing.Graphics object we can set our resizing options and then draw our thumbnail. With this step we have resized our image but it is as a bitmap.
Dim ScaleFactor As Double = TemporaryImage.Width / TargetWidth ThumbImage = New Bitmap(TargetWidth, CInt(TemporaryImage.Height / ScaleFactor)) Dim DescinationRectangle As New Rectangle(0, 0, ThumbImage.Width, ThumbImage.Height) Dim GraphicsCrop As Graphics = Graphics.FromImage(ThumbImage) GraphicsCrop.SmoothingMode = SmoothingMode.HighQuality GraphicsCrop.CompositingQuality = CompositingQuality.HighQuality GraphicsCrop.InterpolationMode = InterpolationMode.High GraphicsCrop.DrawImage(TemporaryImage, DescinationRectangle, 0, 0, TemporaryImage.Width, TemporaryImage.Height, GraphicsUnit.Pixel
After resizing our bitmap image successfully we need to save it as a jpeg image. From the System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders() array we get all the available System.Drawing.Imaging.ImageCodecInfo objects. Looping that array we can find the “JPEG” ImageCodecInfo.
Dim arrayICI() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders() Dim jpegICI As ImageCodecInfo = Nothing Dim x As Integer = 0 For x = 0 To arrayICI.Length - 1 If (arrayICI(x).FormatDescription.Equals("JPEG")) Then jpegICI = arrayICI(x) Exit For End If Next
Then we set some EncoderParameters. These parameters will define the quality of the jpeg we want to create. I use the LZW compression , set quality level to 95, and color depth to 60L. You can change these parameters or use more in order to get the desired result.
Dim myEncoderParameters As New EncoderParameters(3) myEncoderParameters.Param(0) = New EncoderParameter(Encoder.Compression, EncoderValue.CompressionLZW) myEncoderParameters.Param(1) = New EncoderParameter(Encoder.Quality, 95) myEncoderParameters.Param(2) = New EncoderParameter(Encoder.ColorDepth, 60L) ThumbImage.Save(DestinationImagePath, jpegICI, myEncoderParameters) ThumbImage.Dispose() TemporaryImage.Dispose() End Sub
Be sure to call the Dispose() function to the Image object, because the source file remains locked. This methods works for JPG image files. It also works with .png files but if the .png files has transparent colors a black background is put and that messes the image. Also, this method strips all meta data (date taken, camera model, GPS coordinates, etc) an image might have.
Leave a Reply