Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    708

    Upload resize image VB + qualità immagine

    Ciao

    ho trovato in rete un upload resize image in VB:

    http://snipplr.com/view/44271/

    l'ho provato e funziona ma NON per la qualità delle immagini, ovvero, caricando una foto ne estrapola due, una thumbs e una foto grande, e le carica sul server ma le carica con le dimensioni originali delle foto nell'hard disk, quindi se si carica una foto di 10MB l'upload la carica sul server a 10MB e la piccola viene ridimensionata con le proporzioni della foto grande.

    Vi chiedevo se qualcuno conosce un resize quality dell'immagine in VB, credo che si chiami così, da integrare alla pagina che vi ho postato che faccia un resize della qualità della immagine che aiuterebbe a ridurre il peso in KB delle due immagini mantenendo una qualità dell'immagine accettabile per il web...per capirci una sorta di "Salva per il web" di Photoshop.

    Potete aiutarmi e consigliarmi?

    Pier

  2. #2
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    708
    nessuno può aiutarmi?

  3. #3
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    rispondo qui al tuo messaggio.
    Nel codice di sotto ho semplicemente detto al programma di produrre un'altra immagine ridotta da usare nel web invece della originale.
    Nella directory uploads, dove ho dato i permessi di scrittura, avrò pertanto tre immagini:
    quella originale, la miniatura e quella per il web.
    Le dimensioni si impostano nelle variabili Lx e Ly per la miniatura, e Lx1 e Ly1 per quelle web.

    Alla fine mostro le due immagini ridimensionate e non l'originale.
    Nota: l'ho provato e va, ma il codice è tutto da rifare

    codice:
    <%@ Page Trace="False" Language="vb" AspCompat="false" Debug="true" ValidateRequest="false" %>
    
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.Drawing.Imaging" %>
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Collections" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.UI" %>
    <%@ Import Namespace="System.Web.UI.WebControls" %>
    <script language="VBScript" runat="server">
        Const Lx = 200 ' max width for thumbnails
        Const Ly = 240 ' max height for thumbnails
        Const Lx1 = 800 ' max width for thumbnails
        Const Ly1 = 800 ' max height for thumbnails
        Dim upload_dir = "uploads/" ' directory to upload file
        Dim upload_original = ""
        ' filename to save original as (suffix added by script)
        Dim upload_thumb = "_thumb" ' filename to save thumbnail as (suffix added by script)
        Dim upload_thumb1 = "_thumb1" ' filename to save thumbnail as (suffix added by script)
        Const upload_max_size = 10000 '250 ' max size of the upload (KB) note: this doesn't override any server upload limits
        Dim sFileName = ""
        Dim fileExt ' used to store the file extension (saves finding it mulitple times)
        Dim newWidth, newHeight As Integer ' new width/height for the thumbnail
        Dim newWidth1, newHeight1 As Integer ' new width/height for the thumbnail
        Dim l2 ' temp variable used when calculating new size
        Dim fileFld As HttpPostedFile ' used to grab the file upload from the form
        Dim originalimg As System.Drawing.Image ' used to hold the original image
        Dim msg ' display results
        Dim upload_ok As Boolean ' did the upload work ?
    </script>
    <%
         
         
        Randomize() ' used to help the cache-busting on the preview images
        upload_ok = False
        If LCase(Request.ServerVariables("REQUEST_METHOD")) = "post" Then
            fileFld = Request.Files(0) ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image)
         
            upload_original = fileFld.FileName
         
         
            sFileName = Left(upload_original, Len(upload_original) - 4)
         
         
            If fileFld.ContentLength > upload_max_size * 1024 Then
                msg = "Sorry, the image must be less than " & upload_max_size & "Kb"
            Else
                Try
                    originalimg = System.Drawing.Image.FromStream(fileFld.InputStream)
                    ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
                    ' Note: if the original is smaller than the thumbnail size it will be scaled up
                    If (originalimg.Width / Lx) > (originalimg.Width / Ly) Then
                        l2 = originalimg.Width
                        newWidth = Lx
                        newHeight = originalimg.Height * (Lx / l2)
                        If newHeight > Ly Then
                            newWidth = newWidth * (Ly / newHeight)
                            newHeight = Ly
                        End If
                    Else
                        l2 = originalimg.Height
                        newHeight = Ly
                        newWidth = originalimg.Width * (Ly / l2)
                        If newWidth > Lx Then
                            newHeight = newHeight * (Lx / newWidth)
                            newWidth = Lx
                        End If
                    End If
                    
                    Dim thumb As New Bitmap(newWidth, newHeight)
         
                    'Create a graphics object
                    Dim gr_dest As Graphics = Graphics.FromImage(thumb)
         
                    ' just in case it's a transparent GIF force the bg to white
                    Dim sb = New SolidBrush(System.Drawing.Color.White)
                    gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)
         
                    'Re-draw the image to the specified height and width
                    gr_dest.DrawImage(originalimg, 0, 0, thumb.Width, thumb.Height)
         
                    
                    
                    
                    If (originalimg.Width / Lx1) > (originalimg.Width / Ly1) Then
                        l2 = originalimg.Width
                        newWidth1 = Lx1
                        newHeight1 = originalimg.Height * (Lx1 / l2)
                        If newHeight1 > Ly1 Then
                            newWidth1 = newWidth1 * (Ly1 / newHeight1)
                            newHeight1 = Ly1
                        End If
                    Else
                        l2 = originalimg.Height
                        newHeight1 = Ly1
                        newWidth1 = originalimg.Width * (Ly1 / l2)
                        If newWidth1 > Lx1 Then
                            newHeight1 = newHeight1 * (Lx1 / newWidth1)
                            newWidth1 = Lx1
                        End If
                    End If
    
                    
                    
                    Dim thumb1 As New Bitmap(newWidth1, newHeight1)
         
                    'Create a graphics object
                    Dim gr_dest1 As Graphics = Graphics.FromImage(thumb1)
         
                    ' just in case it's a transparent GIF force the bg to white
                    Dim sb1 = New SolidBrush(System.Drawing.Color.White)
                    gr_dest1.FillRectangle(sb1, 0, 0, thumb1.Width, thumb1.Height)
         
                    'Re-draw the image to the specified height and width
                    gr_dest1.DrawImage(originalimg, 0, 0, thumb1.Width, thumb1.Height)
                    
                    
                    
                    
                    Try
                        fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()
                        originalimg.Save(Server.MapPath(upload_dir & sFileName & fileExt), originalimg.RawFormat)
                        thumb.Save(Server.MapPath(upload_dir & sFileName & upload_thumb & fileExt), originalimg.RawFormat)
                        thumb1.Save(Server.MapPath(upload_dir & sFileName & upload_thumb1 & fileExt), originalimg.RawFormat)
                        msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & sFileName & fileExt)
                        upload_ok = True
                    Catch
                        msg = "Sorry, there was a problem saving the image."
                    End Try
                    ' Housekeeping for the generated thumbnail
                    If Not thumb Is Nothing Then
                        thumb.Dispose()
                        thumb = Nothing
                        thumb1.Dispose()
                        thumb1 = Nothing
                    End If
                Catch
                    msg = "Sorry, that was not an image we could process."
                End Try
            End If
         
            ' House Keeping !
            If Not originalimg Is Nothing Then
                originalimg.Dispose()
                originalimg = Nothing
            End If
         
        End If
    %>
    <html>
    <head>
        <title>ASP.NET File Upload and Resize Sample</title>
        <meta name="Description" content="ASP.NET File Upload and Resize Sample (Hybrid VB.NET)">
        <meta name="Keywords" content="ASP.NET, ASP, NET, VB, VBScript, Image, Upload, Resize, Thumbnail, Constrain, Filesize, File, Size, Free">
        <meta name="Copyright" content="Rufan-Redi Pty Ltd 2005">
        <meta name="Author" content="System developed by Jeremy at http://www.Rufan-Redi.com">
        <style type="text/css">
            .style1
            {
                width: 144px;
            }
        </style>
    </head>
    <body>
        <form id="Form1" enctype="multipart/form-data" method="post" runat="server">
        <table>
            <tr>
                <td class="style1">
                    Select the file to upload:
                </td>
                <td>
                    <input type="file" name="upload_file" id="upload_file" title="upload_file">
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    Max upload size
                    <%=upload_max_size%>Kb, gif/jpg/png only
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="Upload">
                </td>
            </tr>
        </table>
        </form>
        <%
            If upload_ok Then
        %>
        <table>
            <tr>
                <td valign="top">
                    [img]<%=upload_dir & sFileName & upload_thumb1 & fileExt & [/img]">
                </td>
                <td valign="top">
                    [img]<%=upload_dir & sFileName & upload_thumb & fileExt & [/img]">
                </td>
            </tr>
        </table>
        <%
        Else
            Response.Write(msg)
        End If
        %>
    </body>
    </html>
    Pietro

  4. #4
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    708
    Ciao Pietro

    funziona!

    Ti rispondo veloce xchè sto modificando il tuo codice x adattarlo meglio alle mie esigenze e postare un'altra pagina, cmq due domande vorrei fartele:

    la modifica che hai fatto non riguarda la "qualità dell'immagine" ma la creazione di due nuove dimensioni, Lx1 e Ly1, ottenendo il risultato che desideravo ovvero la dimunizione del peso in KB.

    1) allora ti chiedo se mi puoi spiegare, a livello teorico, cosa comporta creare ed aggiungere il codice per la qualità dell'immagine? Cioè, rispetto alla modifica delle dimensioni, la modifica sulla qualità utilizzerebbe maggiori risorse del server e magari non si otterebbe la diminuzione dei KB come nel primo caso? ...o è semplicemente + complicato?

    2) cosa vuoi dire che il codice è tutto da rifare? E' vecchio? Si può migliorare?

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    708
    Posto una versione funzionante che genera sempre 3 foto:

    1) una miniatura
    2) una foto grande con dimensioni originali
    3) una foto grande di qualità web

    ...rispetto alla prima pagina postata ho aggiunto delle varibiabili per la "foto_x_web" facendola salvare nella cartella "public/image/foto":

    codice:
    <%@ Page Trace="False" Language="vb" aspcompat="true" debug="true" validateRequest="false"%>
        
    <%@ Import Namespace=System.Drawing %>
    <%@ Import Namespace=System.Drawing.Imaging %>
    <%@ Import Namespace=System %>
    <%@ Import Namespace=System.Collections %>
    <%@ Import Namespace=System.Web %>
    <%@ Import Namespace=System.Web.UI %>
    <%@ Import Namespace=System.Web.UI.WebControls %>
    
    	<SCRIPT LANGUAGE="VBScript" runat="server">
    	const Lx = 200	' max width for miniaturenails
    	const Ly = 240	' max height for miniaturenails
    	    Const Lx1 = 800 ' max width for miniaturenails
        	Const Ly1 = 800 ' max height for miniaturenails
        Dim upload_miniature_dir = "public/image/miniature/"    ' directory to upload file
        Dim upload_foto_dir = "public/image/foto/"    ' directory to upload file
    	Dim upload_foto_x_web_dir = "public/image/foto/"    ' directory to upload file
        
    	Dim upload_miniature = "_miniatura" ' filename to save miniaturenail as (suffix added by script)
    	Dim upload_original = ""' filename to save original as (suffix added by script)
    	Dim upload_foto_x_web = "__foto_x_web" ' filename to save miniaturenail as (suffix added by script)
        
    	Const upload_max_size = 10240 ' max size of the upload (KB) note: this doesn't override any server upload limits
        Dim sFileName = ""
    	dim fileExt	' used to store the file extension (saves finding it mulitple times)
    	dim newWidth, newHeight as integer ' new width/height for the miniaturenail
    		Dim newWidth1, newHeight1 As Integer ' new width/height for the miniaturenail
    	dim l2' temp variable used when calculating new size
    	dim fileFld as HTTPPostedFile' used to grab the file upload from the form
    	Dim originalimg As System.Drawing.Image	' used to hold the original image
    	dim msg	' display results
    	dim upload_ok as boolean' did the upload work ?
    	</script>
        
    <%
    	randomize() ' used to help the cache-busting on the preview images
    	upload_ok = false
    	if lcase(Request.ServerVariables("REQUEST_METHOD"))="post" then
    	fileFld = Request.Files(0)  ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image)
    	
    	upload_original = fileFld.FileName
    	
    	sFileName = Left(upload_original, Len(upload_original) - 4)
    	
    	if fileFld.ContentLength > upload_max_size * 1024 then
    		msg = "Sorry, the image must be less than " & upload_max_size & "Kb"
    	else
    		try
    			originalImg = System.Drawing.Image.FromStream(fileFld.InputStream)
    			' work out the width/height for the miniaturenail. Preserve aspect ratio and honour max width/height
    			' Note: if the original is smaller than the miniaturenail size it will be scaled up
    			If (originalImg.Width/Lx) > (originalImg.Width/Ly) Then
    				L2 = originalImg.Width
    				newWidth = Lx
    				newHeight = originalImg.Height * (Lx / L2)
    				if newHeight > Ly then
    					newWidth = newWidth * (Ly / newHeight)
    					newHeight = Ly
    				end if
    			Else
    				L2 = originalImg.Height
    				newHeight = Ly
    				newWidth = originalImg.Width * (Ly / L2)
    				if newWidth > Lx then
    					newHeight = newHeight * (Lx / newWidth)
    					newWidth = Lx
    				end if
    			End If
    
                Dim miniature As New Bitmap(newWidth, newHeight)
                'Create a graphics object           
                Dim gr_dest As Graphics = Graphics.FromImage(miniature)
    			' just in case it's a transparent GIF force the bg to white 
    			dim sb = new SolidBrush(System.Drawing.Color.White)
    			gr_dest.FillRectangle(sb, 0, 0, miniature.Width, miniature.Height)
                'Re-draw the image to the specified height and width
    			gr_dest.DrawImage(originalImg, 0, 0, miniature.Width, miniature.Height)
    			
    			'§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
    			'INIZIO Codice foto x web
    			                
                    If (originalimg.Width / Lx1) > (originalimg.Width / Ly1) Then
                        l2 = originalimg.Width
                        newWidth1 = Lx1
                        newHeight1 = originalimg.Height * (Lx1 / l2)
                        If newHeight1 > Ly1 Then
                            newWidth1 = newWidth1 * (Ly1 / newHeight1)
                            newHeight1 = Ly1
                        End If
                    Else
                        l2 = originalimg.Height
                        newHeight1 = Ly1
                        newWidth1 = originalimg.Width * (Ly1 / l2)
                        If newWidth1 > Lx1 Then
                            newHeight1 = newHeight1 * (Lx1 / newWidth1)
                            newWidth1 = Lx1
                        End If
                    End If
    
        
                    Dim foto_x_web As New Bitmap(newWidth1, newHeight1)
         
                    'Create a graphics object
                    Dim gr_dest1 As Graphics = Graphics.FromImage(foto_x_web)
         
                    ' just in case it's a transparent GIF force the bg to white
                    Dim sb1 = New SolidBrush(System.Drawing.Color.White)
                    gr_dest1.FillRectangle(sb1, 0, 0, foto_x_web.Width, foto_x_web.Height)
         
                    'Re-draw the image to the specified height and width
                    gr_dest1.DrawImage(originalimg, 0, 0, foto_x_web.Width, foto_x_web.Height)
    			'FINE Codice foto x web
    			'§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
    			try
    				fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()
                        originalimg.Save(Server.MapPath(upload_foto_dir & sFileName & fileExt), originalimg.RawFormat)
                        miniature.Save(Server.MapPath(upload_miniature_dir & sFileName & upload_miniature & fileExt), originalimg.RawFormat)
    					foto_x_web.Save(Server.MapPath(upload_foto_x_web_dir & sFileName & upload_foto_x_web & fileExt), originalimg.RawFormat)
    					
                        msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_miniature_dir & sFileName & fileExt)
    				upload_ok = true
    			catch
    				msg = "Sorry, there was a problem saving the image."
    			end try
    			' Housekeeping for the generated miniaturenail
    			if not miniature is nothing then
    				miniature.Dispose()
                    miniature = Nothing
                    foto_x_web.Dispose()
                    foto_x_web = Nothing
    			end if
    		catch
    			msg = "Sorry, that was not an image we could process."
    		end try
    	end if
    	' House Keeping !
    	if not originalImg is nothing then
    		originalImg.Dispose()
    		originalImg = nothing
    	end if
    end if
    %>
    
    <html>
    <head>
    <title>ASP.NET File Upload and Resize Sample</title>
    <META NAME="Description" CONTENT="ASP.NET File Upload and Resize Sample (Hybrid VB.NET)">
    <META NAME="Keywords" CONTENT="ASP.NET, ASP, NET, VB, VBScript, Image, Upload, Resize, miniaturenail, Constrain, Filesize, File, Size, Free">
    <META NAME="Copyright" CONTENT="Rufan-Redi Pty Ltd 2005">
    <META NAME="Author" CONTENT="System developed by Jeremy at http://www.Rufan-Redi.com">
        <style type="text/css">
            .style1 {
                width: 144px;
            }
        </style>
    </head>
    <body>
    <form id="Form1" enctype="multipart/form-data" method="post" runat="server">
    <table>
    <tr><td class="style1">Seleziona un'immagine:</td><td>
        <input type="file" name="upload_file" 
            id="upload_file" title="upload_file"></td></tr>
    <tr><td colspan=2>Max upload size <%=upload_max_size%>Kb, gif/jpg/png only</td></tr>
    <tr><td colspan=2><input type="submit" value="Upload"></td></tr>
    </table>
    </form>
    
    <% if upload_ok then %>
    
    <table>
    <tr>
    <td valign=top>[img]<%=upload_foto_x_web_dir & sFileName & fileExt & [/img]"></td>
    <td valign=top>[img]<%=upload_miniature_dir & sFileName & upload_miniature & fileExt & [/img]"></td>
    </tr>
    </table>
    <%
    else
    	response.write(msg)
    end if
    %>
    </body>
    </html>
    Il mio prossimo obiettivo è collegare questa pagina ad una connessione per db access ma aspetto le risposte alle prime due domande x vedere se si può implementare il codice in altro modo!

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    708
    Visto che adattare questo script x asp classico è MOLTO problematico propongo una soluzione più fattibile.

    La mia idea di partenza era di integrare questo upload resize image in ASP.NET VB ad una pagina asp classico che inserisce dati in un db access.

    Scrivendo altri post mi hanno sconsigliato di procedere in questo senso xchè adattare l'asp classico, con la direttiva aspcompat="true", all'asp.net sarebbe stato un lavoraccio con la possibilità che il risultato non sia funzionate!

    Tornando alla mia proposta fattibile, chiedo se si potrebbe lasciare la pagina inserimento in asp classico così com'è, senza il codice dell'upload resize image e se si vuole inserire un immagine si richiama la pagina aspx con lo script upload che conterrà il tasto x selezionare l'immagine e alcuni campi testo come ad es. Cognome e Nome.

    Dopo l'upload si fa chiudere la pagina aspx e i VALORI dei percorsi dell'immagine con i due nomi dell'immagine, del Cognome e Nome si fanno salvare negli stessi campi dell'asp classico e poi si procederà con l'invio dei dati della pagina asp.

    Sarebbe da modificare solo il file aspx aggiungendo la connessione ad un db access, i campi Cognome e Nome e aggiungere il codice per salvare i valori delle variabili nel file asp.

    Alla fine le variabili da salvare sono:
    IMG_TBN_1 'percorso thumb + nome immagine
    IMG_1 'percorso immagine grande + nome immagine
    COGNOME
    NOME

    Mi aiutate?

  7. #7
    Utente di HTML.it
    Registrato dal
    May 2003
    Messaggi
    708
    ...faccio una rettifica!

    Non c'è bisogno di inserire i campi Cognome e Nome nell'aspx xchè si possono mettere nella pagina asp. Quindi nel upload resize image si deve inserire solo un campo sfoglia e salvare i percorsi delle deu immagini:

    percorso thumb + nome immagine --> IMG_TBN_1
    percorso img grande + nome immagine --> IMG_1

    ...magari si potrebbe inserire 2 o 3 campi sfoglia...ma intanto partiamo con 1!

  8. #8
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Tanto tempo fa avevo fatto una prova con una pagina asp che spediva files e campi testo ad una pagina aspx.

    In pratica è il caso di chi usa e vuole continuare a usare asp.
    In questo caso ci si procura una dll e una paginetta aspx che fa il lavoro che asp non è in grado di fare (upload e resize immagini)

    In pratica, la pagina asp ha varii campi testo e i tasti sfoglia per la spedizione files.
    Il submit viene fatto alla pagina aspx specializzata.
    La pagina aspx fa il suo lavoro e fa il redirect alla pagina asp spedendo i dati per la memorizzazione nel database.

    Questa è la traccia: il lavoro però spetta a te
    Pietro

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.