A couple points of interest. The code encodes the thumbnail at 75% quality. If the original image is smaller than the requested width and height, it won't scale it but will send it to the browser.
1: Public Sub ScaledImage(ByVal Path As String, ByVal Width As Integer, ByVal Height As Integer)
2: Dim originalWidth As Integer
3: Dim originalHeight As Integer
4: Dim image As bitmap = bitmap.FromFile(Path)
5:
6: originalHeight = image.Height
7: originalWidth = image.Width
8:
9: Dim heightRatio As Double = CDbl(originalHeight / originalWidth)
10: Dim widthRatio As Double = CDbl(originalWidth / originalHeight)
11:
12: Dim desiredHeight As Integer = Height
13: Dim desiredWidth As Integer = Width
14:
15: Height = desiredHeight
16: Width = Convert.ToInt32(Height * widthRatio)
17: If Width > desiredWidth Then
18: Width = desiredWidth
19: Height = Convert.ToInt32(Width * heightRatio)
20: End If
21:
22: If Height > originalHeight Or Width > originalWidth Then
23: Height = originalHeight
24: Width = originalWidth
25:
26: image.Save(Response.OutputStream, ImageFormat.Jpeg)
27: Else
28: Dim thumb As Graphics
29: Dim bitmap As bitmap = New bitmap(Width, Height)
30: thumb = Graphics.FromImage(bitmap)
31: thumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
32: thumb.DrawImage(image, 0, 0, Width, Height)
33:
34: Dim codec As ImageCodecInfo = GetEncoderInfo("image/jpeg") 35:
36: Dim eps As EncoderParameters = New EncoderParameters(1)
37: eps = New EncoderParameters
38: eps.Param(0) = New EncoderParameter(Encoder.Quality, CLng(75))
39:
40: bitmap.Save(Response.OutputStream, codec, eps)
41:
42: thumb.Dispose()
43: bitmap.Dispose()
44: eps.Dispose()
45: End If
46: image.Dispose()
47:
48: Response.ContentType = "image/pjpeg"
49: Response.End()
50: End Sub
51:
52: Public Function GetEncoderInfo(ByVal MimeType As String) As ImageCodecInfo
53: Dim myEncoders() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
54: Dim myEncoder As ImageCodecInfo
55: For Each myEncoder In myEncoders
56: If myEncoder.MimeType = MimeType Then
57: Return myEncoder
58: End If
59: Next
60: Return Nothing
61: End Function
Works for me.