<%@ Page Language="VB" Debug="False" Trace="False" %>
<%@Import Namespace="System.Drawing" %>
<%@Import Namespace="System.Drawing.Imaging" %>
<%@Import Namespace="System.Drawing.Drawing2D" %>
<script language="VB" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strImage As String = Replace(Request.QueryString("image"), "%20", " ")
Dim strAlbum as String = Replace(Request.QueryString("album"), "%20", " ")
If strImage = String.Empty Or strAlbum = String.Empty Then Exit Sub
If InStr(strImage, "/") <> 0 Or InStr(strImage, "\") <> 0 Or _
InStr(strAlbum, "/") <> 0 Or InStr(strAlbum, "\") <> 0 Then
Exit Sub
End If
Dim sWidth = Request("w")
Dim sHeight = Request("h")
If Not sHeight = String.Empty And Not sWidth = String.Empty Then
sWidth = Integer.Parse(sWidth)
sHeight = Integer.Parse(sHeight)
Else
sWidth = 20
sHeight = 20
End If
Dim strPath As String = Server.MapPath("photos/" & strAlbum & "/" & strImage)
Dim fsMain As System.IO.FileStream = New System.IO.FileStream(strPath, _
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read)
'Cache.Remove(strPath & "_" & sWidth & "_" & sHeight)
Dim bytImage As Byte() = Cache.Get(strPath & "_" & sWidth & "_" & sHeight)
Dim msTempStream As System.IO.MemoryStream
Dim imgMain As System.Drawing.Image
If bytImage Is Nothing Then
ReDim bytImage(fsMain.Length)
fsMain.Read(bytImage, 0, fsMain.Length)
fsMain.Close()
msTempStream = New System.IO.MemoryStream(bytImage)
imgMain = System.Drawing.Image.FromStream(msTempStream)
Dim lnRatio As Decimal
Dim lnNewWidth As Integer = 0
Dim lnNewHeight As Integer = 0
If (imgMain.Width > imgMain.Height) Then
lnRatio = Integer.Parse(sWidth) / imgMain.Width
lnNewWidth = Integer.Parse(sWidth)
Dim lnTemp As Decimal = imgMain.Height * lnRatio
lnNewHeight = lnTemp
Else
lnRatio = Integer.Parse(sHeight) / imgMain.Height
lnNewHeight = Integer.Parse(sHeight)
Dim lnTemp As Decimal = imgMain.Width * lnRatio
lnNewWidth = lnTemp
End If
Dim imgThumbnail As System.Drawing.Image = New Bitmap(lnNewWidth, lnNewHeight)
Dim graThumbnail As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(imgThumbnail)
graThumbnail.InterpolationMode = InterpolationMode.HighQualityBicubic
graThumbnail.SmoothingMode = SmoothingMode.HighQuality
graThumbnail.PixelOffsetMode = PixelOffsetMode.HighQuality
graThumbnail.CompositingQuality = CompositingQuality.HighQuality
graThumbnail.DrawImage(imgMain, 0, 0, lnNewWidth, lnNewHeight)
Dim iciInfo() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
Dim encpMain As EncoderParameters = New EncoderParameters(1)
encpMain.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L)
Dim strOutputFilename As String = String.Empty
strOutputFilename = Request.QueryString("OutputFilename")
If Not strOutputFilename = String.Empty Then
Try
imgThumbnail.Save(Server.MapPath("photos/" & strAlbum & "/") & strOutputFilename, ImageFormat.Jpeg)
Catch ex As Exception
imgThumbnail.Dispose()
Return
End Try
End If
Response.ContentType = "image/jpeg"
Dim msSaveStream As System.IO.MemoryStream = New System.IO.MemoryStream
imgThumbnail.Save(msSaveStream, iciInfo(1), encpMain)
msSaveStream.WriteTo(Response.OutputStream)
Dim bytSave(msSaveStream.Length) As Byte
msSaveStream.Position = 0
msSaveStream.Read(bytSave, 0, msSaveStream.Length)
Cache.Insert(strPath & "_" & sWidth & "_" & sHeight, _
bytSave, Nothing, DateTime.MaxValue, TimeSpan.Zero)
imgThumbnail.Dispose()
Else
msTempStream = New System.IO.MemoryStream(bytImage)
Response.ContentType = "image/jpeg"
msTempStream.WriteTo(Response.OutputStream)
Dim strOutputFilename As String = String.Empty
strOutputFilename = Request.QueryString("OutputFilename")
If Not strOutputFilename = String.Empty Then
Try
imgMain = System.Drawing.Image.FromStream(msTempStream)
imgMain.Save(Server.MapPath("photos/" & strAlbum & "/") & strOutputFilename, ImageFormat.Jpeg)
Catch ex As Exception
imgMain.Dispose()
Return
End Try
imgMain.Dispose()
End If
End If
End Sub
</script>