Prashant Verma Prashant Verma - 6 months ago 7
Vb.net Question

I have to implement this function in my project while I add this function I get error near new bitmap

Public Shared Function ScaleImage(image As System.Drawing.Image, maxHeight As Integer) As System.Drawing.Image
Dim ratio = CDbl(maxHeight) / image.Height
Dim newWidth = CInt(Math.Truncate(image.Width * ratio))
Dim newHeight = CInt(Math.Truncate(image.Height * ratio))
Dim newImage = **New Bitmap(newWidth, newHeight)**
Using g = Graphics.FromImage(newImage)
g.DrawImage(image, 0, 0, newWidth, newHeight)
End Using
Return newImage
End Function

Answer

You're currently declaring everything as Object, the compiler does not know which type of variable you want.

You should always ensure to include that in the declaration in order to reduce the chance of complications such as this. This is what the As keyword is used for.

Dim ratio As Double = CDbl(maxHeight) / image.Height
Dim newWidth As Integer = CInt(Math.Truncate(image.Width * ratio))
Dim newHeight As Integer = CInt(Math.Truncate(image.Height * ratio))
Dim newImage As New Bitmap(newWidth, newHeight)
Using g As Graphics = Graphics.FromImage(newImage)
    g.DrawImage(image, 0, 0, newWidth, newHeight)
End Using
Return newImage