Introduction
Sometime when we are uploading images and if we resize with applying variable size then images are not displayed in good quality. So, this article show how to resize images without scratch the image.
Background
In this article we have to use some drawing methods.
Using the code
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(Stream)
Dim oldWidth As Integer = image.Width
Dim oldHeight As Integer = image.Height
If (CDec(oldWidth) / CDec(oldHeight)) > (CDec(newWidth) / CDec(newHeight)) Then
Dim ratio As Decimal = CDec(newWidth) / oldWidth
newHeight = CInt((oldHeight * ratio))
Else
Dim ratio As Decimal = CDec(newHeight) / oldHeight
newWidth = CInt((oldWidth * ratio))
End If
Dim bitmap As New Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb)
bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution)
Dim graphics__1 As Graphics = Graphics.FromImage(bitmap)
graphics__1.Clear(Color.White)
graphics__1.InterpolationMode = InterpolationMode.HighQualityBicubic
graphics__1.DrawImage(image, New Rectangle(0, 0, newWidth, newHeight),
New Rectangle(0, 0, oldWidth, oldHeight), GraphicsUnit.Pixel)
graphics__1.Dispose()
bitmap.Save(Server.MapPath("Images/") & strDestinationFileName, image.RawFormat)