resize image function outputs a distorted image
I have a function that I want to pass the image and size to resize image. The issue I am seeing is that the original image looks very clear but the resized image is very distorted. I could see for a image enlarging but I am resizing to a much smaller image.
Here is my function:
Code:
Public Function ResizeImage(ByVal sFileName As String, Optional iWidth As Integer = 230, Optional iHeight As Integer = 320) As Byte()
Dim sFullFilePath As String = "http://www.domain.com/images/" & sFileName
Dim _width As Integer = iWidth
Dim _height As Integer = iHeight
'Set maximum width and height of image
Dim MaxWidth As Integer = _width
Dim MaxHeight As Integer = _height
'Create a plain white box
Dim BlankImg As Bitmap = New Bitmap(_width, _height)
Dim BlankGraphic As Graphics = Graphics.FromImage(BlankImg)
BlankGraphic.FillRectangle(Brushes.White, New Rectangle(0, 0, BlankImg.Width, BlankImg.Height))
BlankGraphic.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
BlankGraphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
BlankGraphic.PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality
BlankGraphic.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
'Load image from web to add to new Bitmap
Dim oPost As WebPost = New WebPost
Dim s As Stream = New MemoryStream(oPost.SaveImageToByte(sFullFilePath)) 'download image to stream
Dim OriginalImg As Bitmap = System.Drawing.Bitmap.FromStream(s)
Dim ImgFormat As ImageFormat = OriginalImg.RawFormat 'grab the images format to save bitmap as later
'Resize image if either width or height is larger than the maximum allowed
Dim iDiff As Integer = 0
Dim newX As Integer = 0 'Set the new top left drawing position on the image canvas
Dim newY As Integer = 0
Dim wRatio As Decimal = 0
Dim newWidth As Integer = _width
Dim newHeight As Integer = _height
If (OriginalImg.Width > MaxWidth) Or (OriginalImg.Height > MaxHeight) Then
'Need to determine what dimension is larger.
Dim iWidthDiff As Integer = OriginalImg.Width - MaxWidth
Dim iHeigthDiff As Integer = OriginalImg.Height - MaxHeight
If iWidthDiff > iHeigthDiff Then
iDiff = OriginalImg.Width - MaxWidth
newHeight = Convert.ToInt64(OriginalImg.Height - ((iDiff / OriginalImg.Width) * OriginalImg.Height))
newWidth = MaxWidth
newX = 0
newY = Int((_height - newHeight) / 2)
Else
iDiff = OriginalImg.Height - MaxHeight
newWidth = Convert.ToInt64(OriginalImg.Width - ((iDiff / OriginalImg.Height) * OriginalImg.Width))
newHeight = MaxHeight
newX = Int((_width - newWidth) / 2)
newY = 0
End If
Else
newHeight = OriginalImg.Height
newWidth = OriginalImg.Width
newX = Int((_width - newWidth) / 2) 'Position the image centrally across the canvas
newY = Int((_height - newHeight) / 2) 'Position the image centrally down the canvas
End If
'Add original image to graphic
BlankGraphic.DrawImage(OriginalImg, newX, newY, newWidth, newHeight)
Dim codecEncoder As ImageCodecInfo
codecEncoder = GetEncoder("image/jpeg")
Dim quality As Integer = 100
Dim encodeParams As New EncoderParameters(1)
Dim qualityParam As New EncoderParameter(Imaging.Encoder.Quality, quality)
encodeParams.Param(0) = qualityParam
Dim imageBytes As Byte() = Nothing
Using memoryStream As New MemoryStream()
BlankImg.Save(memoryStream, codecEncoder, encodeParams)
imageBytes = memoryStream.ToArray()
End Using
Return imageBytes
End Function
Function GetEncoder(ByVal mimeType As String) As ImageCodecInfo
Dim codec As ImageCodecInfo
Dim RetCodec As ImageCodecInfo = Nothing
Dim codecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
For Each codec In codecs
If codec.MimeType = mimeType Then
RetCodec = codec
End If
Next
Return RetCodec
End Function
Here is how I am calling this:
Code:
Dim oImgFunc As image_functions = New image_functions
Dim s As Byte() = oImgFunc.ResizeImage(sSrcImg, 140, 170)
Dim imgItem As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(s)
pCell.AddElement(New Phrase(New Chunk(imgItem, 0, 0)))
This version returns much clearer but images can be distorted because unlike the ResizeImage which will re-size proportionally.
Code:
Dim sFullImgUrl As String = "http://www.domain.com/images/" & sSrcImg
Dim oPost As WebPost = New WebPost
Dim s As Stream = New MemoryStream(oPost.SaveImageToByte(sFullImgUrl))
Dim imgItem As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(s)
imgItem.ScaleAbsolute(140, 170)
imgItem.SetDpi(140, 170)
pCell.AddElement(New Phrase(New Chunk(imgItem, 0, 0)))
Any ideas why my ResizeImage isn't returning a clear image?