Here's some code i use in an asp.net app i wrote:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim connstr As String = Application("ConnectionString").ToString
Dim cnn As New SqlConnection(connstr)
Dim cmd As New SqlCommand("select * from t_ezImages where ID=" & Request.QueryString("id"), cnn)
cnn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim strImageType = dr.GetValue(11)
Dim bindata() As Byte = dr.GetValue(6)
Dim Thumbnailbindata() As Byte = resizeImage(bindata)
Response.BinaryWrite(Thumbnailbindata)
End Sub
'*******************************************************
Public Function resizeImage(ByVal pic() As Byte) As Byte()
' Const THUMBNAIL_IMAGE_PATH As String = "C:\Thumbnails\Test.jpg"
Const maxWidth As Integer = 100
Const maxHeight As Integer = 100
Dim inp As New IntPtr
Dim imgHeight, imgWidth As Double
Try
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(pic))
Dim bm = New Bitmap(image)
Dim imgHres, imgVres As Single
'Added this for testing - usually 96 dpi for every picture
imgHres = bm.horizontalresolution
imgVres = bm.verticalresolution
imgHeight = bm.Height
imgWidth = bm.Width
If imgWidth > maxWidth Or imgHeight > maxHeight Then
'Determine what dimension is off by more
Dim deltaWidth As Double = imgWidth - maxWidth
Dim deltaHeight As Double = imgHeight - maxHeight
Dim scaleFactor As Double
If deltaHeight > deltaWidth Then
'Scale by the height
scaleFactor = maxHeight / imgHeight
Else
'Scale by the Width
scaleFactor = maxWidth / imgWidth
End If
imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If
Dim w As Integer = Convert.ToInt32(imgWidth)
Dim h As Integer = Convert.ToInt32(imgHeight)
Dim imgbmp As System.Drawing.Image = bm.GetThumbnailImage(w, h, Nothing, inp)
Dim ms As New MemoryStream
imgbmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim b(ms.Length - 1) As Byte
ms.Position = 0
ms.Read(b, 0, ms.Length)
Return b
'bitmap.Save(THUMBNAIL_IMAGE_PATH, Imaging.ImageFormat.Jpeg)
Catch ex As Exception
End Try
End Function 'resizeImage