Thread: Image resizing
View Single Post
  #2 (permalink)  
Old 07-01-2009, 1:53 AM
Shemeer Shemeer is offline
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Apr 2009
Posts: 17
Reputation: 13
Shemeer is on a distinguished programming path ahead
Smile Resize image

Hi,
Try this

Private Function ResizeImage(ByVal mg As Bitmap, ByVal newSize As Size) As Bitmap
Dim ratio As Double = 0.0R
Dim myThumbWidth As Double = 0.0R
Dim myThumbHeight As Double = 0.0R
Dim x As Integer = 0
Dim y As Integer = 0

Dim bp As Bitmap


If (mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height / Convert.ToDouble(newSize.Height)) Then
ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width)
Else
ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height)
End If
myThumbHeight = Math.Ceiling(mg.Height / ratio)
myThumbWidth = Math.Ceiling(mg.Width / ratio)

Dim thumbSize As New Size(CInt(myThumbWidth), CInt(myThumbHeight))
bp = New Bitmap(newSize.Width, newSize.Height)
x = (newSize.Width - thumbSize.Width) / 2
y = (newSize.Height - thumbSize.Height)
' Had to add System.Drawing class in front of Graphics ---
Dim g As System.Drawing.Graphics = Graphics.FromImage(bp)
g.SmoothingMode = SmoothingMode.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.PixelOffsetMode = PixelOffsetMode.HighQuality
Dim rect As New Rectangle(x, y, thumbSize.Width, thumbSize.Height)
g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, _
GraphicsUnit.Pixel)



Return bp
End Function
Reply With Quote