图像调整器缓存

时间:2014-03-20 00:53:30

标签: vb.net image caching resize

我有一个非常酷的图像缩放器,它可以动态调整图像大小,但我正在尝试修改它以便它可以缓存图像。

我已经搜索了一些关于如何将Response.OutputStream保存到文件的小时数,但我尝试过的所有内容都没有用。

以下是代码:

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Drawing" %>
<%@Import Namespace="System.Drawing.Imaging" %>
<script language="VB" runat="server">
Function ThumbnailCallback() as Boolean
    Return False
End Function

Sub Page_Load(sender as Object, e as EventArgs)

    'Read in the image filename to create a thumbnail of    
    Dim imageUrl as string = Request.QueryString("img")

    'Read in the width and height
    Dim imageHeight as Integer = Request.QueryString("h")
    Dim imageWidth as Integer = Request.QueryString("w")

    Dim fullImg
    Dim currDir

    imageUrl = server.MapPath(imageUrl)
    fullimg = imageUrl

    Dim fullSizeImg as System.Drawing.Image
    fullSizeImg = System.Drawing.Image.FromFile(imageURL)
    fullSizeImg.RotateFlip(RotateFlipType.Rotate180FlipY)
    fullSizeImg.RotateFlip(RotateFlipType.Rotate180FlipY)

    Response.ContentType = "image/JPEG"

    If imageHeight > 0 And imageWidth > 0 Then

        Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
        Dim thumbNailImg As System.Drawing.Image

        dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

        ' if new image size is less than current image size
        if (imageWidth < fullSizeImg.Width) or (imageHeight < fullSizeImg.Height) then

            If (fullSizeImg.Width / imageWidth) > (fullSizeImg.Height / imageHeight) Then
                'imageHeight = Round(fullSizeImg.Height * (imageWidth / fullSizeImg.Width), 0)
                imageHeight = fullSizeImg.Height * (imageWidth / fullSizeImg.Width)
                imageWidth = imageWidth

            Else

                'no need to modify height
                'imageWidth = Round(fullSizeImg.Width * (imageHeight / fullSizeImg.Height), 0)
                imageWidth = fullSizeImg.Width * (imageHeight / fullSizeImg.Height)

            End If

            thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero)
            thumbNailImg.Save(Response.OutputStream, ImageFormat.Jpeg)
            thumbNailImg.Dispose()
        else

            fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg)
            fullSizeImg.Dispose()

        end if

    Else

        fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg)
        fullSizeImg.Dispose()

    End If
End Sub
</script>

1 个答案:

答案 0 :(得分:0)

是否有理由需要使用response.output流并且无法直接保存到文件中?从system.image文档中可以看出,您应该能够直接保存到文件中。 http://msdn.microsoft.com/en-us/library/ktx83wah(v=vs.110).aspx