ciao
premetto che non capisco molto di asp.net.
ho trovato in rete un resize che uso in una galleria di immagini... ecco il codice:
codice:
<%@ Page Language="VB" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Drawing" %>
<script runat="server">
'Copyright (C) 2007 Daniele Iunco
'This program is free software; you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation; either version 2 of the License, or
'(at your option) any later version.
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
'You should have received a copy of the GNU General Public License along
'with this program; if not, write to the Free Software Foundation, Inc.,
'51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
'resizetool.aspx v1.0
'Author: Daniele Iunco
'Date: 9th May 2007
'Limite massimo per l'altezza in pixel
Private Const MaxHeightSafe As Integer = 800
'Limite massimo per la larghezza in pixel
Private Const MaxWidthSafe As Integer = 800
'Percorso dell'immagine su cui reindirizzare le richieste non valide
Private Const ErrorImagePath As String = "/error.jpg"
'Percorso in cui salvare le copie delle immagini, il percorso indicato deve avere i permessi in scrittura
Private Const ImageCachePath As String = "/public/tmp_photo/preview_thumb_sofia_tmp"
'Salva su disco una copia riutilizzabile delle thumbnails create
Private Const UseCache As Boolean = True
'Di seguito sono riportati i valori predefiniti che verranno utilizzati quando non vengono specificati parametri
Private Const DefaultHeight As Integer = 450
Private Const DefaultWidth As Integer = 450
Private Const DefaultResizeMode As ResizeMode = ResizeMode.AutoResize
Private Enum ResizeMode
AutoResize = 0
ManualResize = 1
End Enum
Private ReadOnly Property Mode() As ResizeMode
Get
Dim myReqMode As String = Request("mode")
If myReqMode Is Nothing OrElse Not IsNumeric(myReqMode) Then Return DefaultResizeMode
If CInt(myReqMode) = 0 Then Return ResizeMode.AutoResize Else Return ResizeMode.ManualResize
End Get
End Property
Private ReadOnly Property MaxHeight() As Integer
Get
Dim myValue As String = Request("height")
If myValue Is Nothing OrElse Not IsNumeric(myValue) Then Return DefaultHeight
Dim myIntValue As Integer = CInt(myValue)
If myIntValue < MaxHeightSafe Then
Return myIntValue
Else
Return MaxHeightSafe
End If
End Get
End Property
Private ReadOnly Property MaxWidth() As Integer
Get
Dim myValue As String = Request("width")
If myValue Is Nothing OrElse Not IsNumeric(myValue) Then Return DefaultWidth
Dim myIntValue As Integer = CInt(myValue)
If myIntValue < MaxWidthSafe Then
Return myIntValue
Else
Return MaxWidthSafe
End If
End Get
End Property
Private _ImagePath As String
Private ReadOnly Property ImagePath() As String
Get
If Not _ImagePath Is Nothing Then Return _ImagePath
Dim sRequestPath As String = Request.QueryString("path")
If sRequestPath Is Nothing Then Return Nothing
'Rimuovo le doppie / e i doppi . con le espressioni regolari
sRequestPath = Regex.Replace(sRequestPath, "\/{2,}", "/")
sRequestPath = Regex.Replace(sRequestPath, "\.{2,}", ".")
If sRequestPath.Substring(0, 1) = "/" Then sRequestPath = sRequestPath.Substring(1)
_ImagePath = sRequestPath
Return _ImagePath
End Get
End Property
Sub Page_Load(ByVal Sender As Object, ByVal e As System.EventArgs)
Dim LocalImageFilePath As String
Dim LocalThumbnailCacheFilePath As String
LocalImageFilePath = Server.MapPath("/" & ImagePath)
If Not File.Exists(LocalImageFilePath) Then
Response.Redirect(ErrorImagePath)
Exit Sub
End If
Dim TempFilePath As String = Server.MapPath(ImageCachePath)
LocalThumbnailCacheFilePath = Path.Combine(TempFilePath, ImagePath.Replace("/", "_")) & "_" & Int(Mode) & "_" & Int(MaxWidth) & "_" & Int(MaxWidth) & ".thumb"
Dim mySourceFile As New System.IO.FileInfo(LocalImageFilePath)
If UseCache Then
If File.Exists(LocalThumbnailCacheFilePath) Then
Dim myDestFile As System.IO.FileInfo
myDestFile = New System.IO.FileInfo(LocalThumbnailCacheFilePath)
'Se il file della thumbnail è stato modificato dopo il file sorgente
'allora è valido e uso quello
If myDestFile.LastWriteTime.CompareTo(mySourceFile.LastWriteTime) = 1 Then
'Il file esiste e va bene
Response.ContentType = "image/jpeg"
Response.WriteFile(myDestFile.FullName)
Response.End()
Exit Sub
Else
'Il file esiste ma è vecchio e va sostituito
myDestFile.Delete()
End If
End If
End If
Dim orginalimg, thumb As System.Drawing.Image
Try
orginalimg = System.Drawing.Image.FromFile(mySourceFile.FullName)
Catch
Response.Redirect(ErrorImagePath)
Exit Sub
End Try
Dim ThumbnailWidth, ThumbnailHeight As Integer
Select Case Mode
Case ResizeMode.AutoResize
' ResizeMode.AutoResize: Ridimensiona l'immagine per restare nei limiti impostati, mantenendo le proporzioni
Dim myRatio As Double = orginalimg.Width / orginalimg.Height
If myRatio > 1 Then 'La larghezza è maggiore dell'altezza, quindi fisso l'altezza e calcolo il resto
ThumbnailHeight = MaxHeight
ThumbnailWidth = orginalimg.Width / orginalimg.Height * MaxHeight
ElseIf myRatio < 1 Then 'L'altezza è maggiore della larghezza, quindi fisso la larghezza e calcolo il resto
ThumbnailWidth = MaxWidth
ThumbnailHeight = orginalimg.Height / orginalimg.Width * MaxWidth
ElseIf myRatio = 1 Then 'Larghezza = Altezza
ThumbnailWidth = MaxWidth
ThumbnailHeight = MaxHeight
End If
Case ResizeMode.ManualResize
' ResizeMode.ManualResize: Ridimensiona con le misure indicate nei parametri
ThumbnailWidth = MaxWidth
ThumbnailHeight = MaxHeight
End Select
Response.ContentType = "image/jpeg"
'Creo l'immagine con le misure calcolate, in formato Jpeg
thumb = orginalimg.GetThumbnailImage(ThumbnailWidth, ThumbnailHeight, Nothing, IntPtr.Zero)
If UseCache Then
Dim myS As New IO.FileStream(LocalThumbnailCacheFilePath, IO.FileMode.CreateNew)
thumb.Save(myS, System.Drawing.Imaging.ImageFormat.Jpeg)
myS.Close()
orginalimg.Dispose()
thumb.Dispose()
Response.WriteFile(LocalThumbnailCacheFilePath)
Else
thumb.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
thumb.Dispose()
End If
End Sub
</script>
ogni volta che apro la pagina che contiene le preview lui fa il resize e salva una copia nella cartella che gli ho settato, pero quando mi ricollego anzicche caricarsi le miniature che si è creato ... fa nuovamente il resize.
risultato: ci impiega sempre un casino a caricare le preview
come posso modificare lo script in maniera che quando richiamo la foto se esiste la preview si carica questa e non rifa tutto il giro?