Image resizing

davco

Member
Joined
Sep 13, 2006
Messages
8
Programming Experience
Beginner
I have an image resizing app and I need to keep the deminsions proportionate.
How do I do that with this code?


Private Sub Reduce(ByVal factor As Double)
img = New Bitmap(img, New Size(50, 50))
picPhoto.Image = img

Dim SizeKb As String
' To compute: size in Kb
Dim ms As New MemoryStream()
img.Save(ms, Imaging.ImageFormat.Jpeg)
SizeKb = (ms.Length \ 1024).ToString() & "Kb "

lblCurrentSize.Text = "Current Size: " & SizeKb & "(" & img.Width & "x" & img.Height & ") [" & img.Width / img.Height & "]"
End Sub
It resizes images well but doesn`t kep the width and height proportionate.
:cool:
 
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
 

Latest posts

Back
Top