Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    ridimensionare immagini da upload ASP

    Salve amici,,

    ho una pagina asp che fa un upload ed alla fine ho il nome del file caricato. vorrei che cliccando sull'immagine si potesse ridimensionare il file salvandolo nuovamente. Ho cercato ed ho trovato questo script:

    <%@ Page Language="VB" ContentType="text/html"%>
    <script language="vb" runat="server">
    Sub Page_Load(Server as Object,e as System.EventArgs)
    Dim redimFoto as String = Request.Querystring("foto")
    Dim RedimSizeThumb as integer = CInt(Request.QueryString("foto"))
    Dim RedimSizeFoto as integer = CInt(Request.QueryString("foto"))
    ReDimImg("" & redimFoto ,"" & RedimFoto,RedimSizeThumb,0)
    ReDimImg("" & redimFoto ,"" & RedimFoto,RedimSizeFoto,0)
    End Sub
    Public Sub ReDimImg(SourcePath as String,ResizeName as string, width as integer,height as integer)
    Try
    'Creo l'oggetto di riferimento al file originale
    Dim TmpBmp as System.Drawing.Bitmap = CType(System.Drawing.Image.FromFile(Server.MapPath (SourcePath)), System.Drawing.Bitmap)
    Dim newWidth,newHeight as string
    'Se il parametro height è lasciato a 0 costruisco il with in maniera proporzionata
    if height = 0 Then
    newHeight = Convert.ToInt32(width / TmpBmp.Width * TmpBmp.Height)
    else
    newHeight = height
    End if
    'Se il parametro with è lasciato a 0 costruisco l'height in maniera proporzionata
    if width = 0 Then
    newWidth = Convert.ToInt32(height / TmpBmp.Height * TmpBmp.Width)
    else
    newWidth = width
    end if
    'Creo la miniatura con lunghezza e larghezza elaborati
    Dim miniaturabmp as New System.Drawing.Bitmap(TmpBmp, newWidth, newHeight)
    'Distruggo l'oggetto dell'immagine originale che non mi serve +
    TmpBmp.Dispose()
    'controllo l'estensione e salvo la miniatura nel giusto formato
    if SourcePath.toLower().EndsWith(".jpg") or SourcePath.toLower().EndsWith(".jpeg") Then
    miniaturabmp.Save(Server.MapPath(ResizeName), System.Drawing.Imaging.ImageFormat.Jpeg)
    elseif SourcePath.toLower().EndsWith(".gif") Then
    miniaturabmp.Save(Server.MapPath(ResizeName), System.Drawing.Imaging.ImageFormat.Gif)
    elseif SourcePath.toLower().EndsWith(".png") Then
    miniaturabmp.Save(Server.MapPath(ResizeName), System.Drawing.Imaging.ImageFormat.Png)
    else
    Response.Write("Errore Formato")
    'Si puo' ampliare l'elseif in questo modo:
    'elseif SourcePath.toLower().EndsWith(".[Estensione]") Then
    'miniaturabmp.Save(Server.MapPath(ResizeName), System.Drawing.Imaging.ImageFormat.[Estensione])
    'dove [Estensione] puo essere Bmp, Emf, Exif, Tiff, Wmf
    end if
    miniaturabmp.Dispose()
    Catch ex as System.Exception
    'Nel caso stampo l'errore:
    Response.Write("<hr>Errore nel ridimensionamento dell'immagine:
    " & ex.Message & "

    ")
    Response.Write("
    " & ex.StackTrace & "<hr>")
    End Try
    Response.Write("Ridimensionamento effettuato con successo")
    End Sub
    </script>

    però non so come adattarlo! Qualcuno saprebbe aiutarmi?

    Ho provato associando un link tipo:
    ridimensiona.aspx?foto=nomefilecaricato

    ma non va
    Digital XP - The new expression of digital art! - http://www.mimanchitu.it

  2. #2
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    372
    guarda io lo ho fatto in vb.net così

    Public Function SetDimensioni(ByVal path As String, ByVal myFile1 As String, ByVal newpath As String) As String
    Dim objGraphics As System.Drawing.Image
    Dim newimgw As Integer
    Dim newimgh As Integer
    Dim intFileNameLength As Integer
    Dim strFileNamePath As String
    Dim strFilename As String
    Dim newex As String

    Dim myUName As String

    Dim i As Integer = 0
    If (myFile1 <> "") Then
    strFileNamePath = myFile1
    intFileNameLength = InStr(1, StrReverse(strFileNamePath), ".")
    Do
    If Mid(strFileNamePath, Len(strFileNamePath) - i, 1) = "\" Then Exit Do
    newname = Mid(strFileNamePath, Len(strFileNamePath) - i, 1) & newname
    i = i + 1
    Loop
    newname = newname.Remove(Len(newname) - InStr(1, StrReverse(strFileNamePath), "."), InStr(1, StrReverse(strFileNamePath), "."))
    newname = newname & ".jpg"
    End If

    Dim b As Bitmap = New Bitmap(myFile1)
    imgw = b.Width
    imgh = b.Height
    newimgw = imgw
    newimgh = imgh

    Dim larghe, alte As Double
    If maxh > newimgh And maxw > newimgw Then
    larghe = newimgw
    alte = newimgh
    Else
    Dim r As Double
    Dim rd As Double
    r = newimgw / newimgh
    rd = maxw / maxh
    If r < rd Then
    larghe = maxh * r
    alte = maxh
    Else
    alte = maxw / r
    larghe = maxw
    End If
    End If

    Dim b2 As Bitmap '= New Bitmap(b, larghe, alte)

    b2 = Resize(b, larghe, alte, True)
    b2.Save(newpath & alte & larghe & newname, System.Drawing.Imaging.ImageFormat.Jpeg)
    Return newpath & alte & larghe & newname
    End Function

    Public Shared Function Resize(ByVal b As Bitmap, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal nboo As Boolean) As Bitmap
    Dim bTemp As Bitmap = CType(b.Clone, Bitmap)
    b = New Bitmap(nWidth, nHeight, bTemp.PixelFormat)
    Dim nXFactor As Double = CType(bTemp.Width, Double) / CType(nWidth, Double)
    Dim nYFactor As Double = CType(bTemp.Height, Double) / CType(nHeight, Double)
    If nboo Then
    Dim fraction_x As Double
    Dim fraction_y As Double
    Dim one_minus_x As Double
    Dim one_minus_y As Double
    Dim ceil_x As Integer
    Dim ceil_y As Integer
    Dim floor_x As Integer
    Dim floor_y As Integer
    Dim c1 As Color = New Color
    Dim c2 As Color = New Color
    Dim c3 As Color = New Color
    Dim c4 As Color = New Color
    Dim red As Byte
    Dim green As Byte
    Dim blue As Byte
    Dim b1 As Byte
    Dim b2 As Byte
    Dim x As Integer = 0
    While x < b.Width
    Dim y As Integer = 0
    While y < b.Height
    floor_x = CType(Math.Floor(x * nXFactor), Integer)
    floor_y = CType(Math.Floor(y * nYFactor), Integer)
    ceil_x = floor_x + 1
    If ceil_x >= bTemp.Width Then
    ceil_x = floor_x
    End If
    ceil_y = floor_y + 1
    If ceil_y >= bTemp.Height Then
    ceil_y = floor_y
    End If
    fraction_x = x * nXFactor - floor_x
    fraction_y = y * nYFactor - floor_y
    one_minus_x = 1 - fraction_x
    one_minus_y = 1 - fraction_y
    c1 = bTemp.GetPixel(floor_x, floor_y)
    c2 = bTemp.GetPixel(ceil_x, floor_y)
    c3 = bTemp.GetPixel(floor_x, ceil_y)
    c4 = bTemp.GetPixel(ceil_x, ceil_y)
    b1 = CType((one_minus_x * c1.B + fraction_x * c2.B), Byte)
    b2 = CType((one_minus_x * c3.B + fraction_x * c4.B), Byte)
    'blue = CType((one_minus_y * CType(CType(fraction_y, b1), Double) * CType((b2), Double)), Byte)
    blue = CType(one_minus_y * CType(b1, Double) + fraction_y * CType(b2, Double), Double)
    b1 = CType((one_minus_x * c1.G + fraction_x * c2.G), Byte)
    b2 = CType((one_minus_x * c3.G + fraction_x * c4.G), Byte)
    'green = CType((one_minus_y * CType(CType(fraction_y, b1), Double) * CType((b2), Double)), Byte)
    green = CType(one_minus_y * CType(b1, Double) + fraction_y * CType(b2, Double), Double)
    b1 = CType((one_minus_x * c1.R + fraction_x * c2.R), Byte)
    b2 = CType((one_minus_x * c3.R + fraction_x * c4.R), Byte)
    'red = CType((one_minus_y * CType(CType(fraction_y, b1), Double) * CType((b2), Double)), Byte)
    red = CType(one_minus_y * CType(b1, Double) + fraction_y * CType(b2, Double), Double)
    b.SetPixel(x, y, System.Drawing.Color.FromArgb(255, red, green, blue))
    System.Threading.Interlocked.Increment(y)
    End While
    System.Threading.Interlocked.Increment(x)
    End While
    Else
    Dim x As Integer = 0
    While x < b.Width
    Dim y As Integer = 0
    While y < b.Height
    b.SetPixel(x, y, bTemp.GetPixel(CType((Math.Floor(x * nXFactor)), Integer), CType((Math.Floor(y * nYFactor)), Integer)))
    System.Threading.Interlocked.Increment(y)
    End While
    System.Threading.Interlocked.Increment(x)
    End While
    End If

    Return b
    End Function

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2002
    Messaggi
    4,127
    per la vista dei frequentatori del forum asp .net la prossima volta che postate del codice utilizzate il tag [CODE]

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.