Uploading and resizing photos

BluJag

Perch
Uploading photos with asp.net

Language is VB

In the .aspx page add the following which allows users to browse to their photo

<INPUT id="fileInput" type="file" size="50" name="fileInput" runat="server">

At the top of your .aspx.vb file you will need the following

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D

Now a subroutine for uploading the photo

Private Sub UploadPhoto()

Dim FileName As String
Dim Msg As String
Dim disc_FileName As String

With fileInput
If .PostedFile.FileName = "" Then
lblFileName.Text = "Please browse to a photo."
Exit Sub
End If

If .PostedFile.ContentLength < 100 Then
lblFileName.Text = "UPLOAD FAILURE 1. - photo too small!"
Exit Sub
End If

If .PostedFile.ContentLength > 204800 Then
lblFileName.Text = "UPLOAD FAILURE 2. - size must not exceed 200k"
Exit Sub
End If

If Right(LCase(.PostedFile.FileName), 3) <> "jpg" Then ' ///////////note you can change this to "gif"
lblFileName.Text = "UPLOAD FAILURE 3. - you may only upload .jpg photo images, max size 200k"
Exit Sub
End If

FileName = "Photo" & ".jpg" '////// or gif
FileName = viewstate("MemberID") & "_" & FileName '///////////// the members ID is stored in Viewstate rather than a session variable.

disc_FileName = Server.MapPath(".\photos\temp\") & LCase(FileName) ' ///////////the directories must of course exist on the server!

.PostedFile.SaveAs(disc_FileName)
End With

'////Now scale the photo

Dim jPeg As Bitmap = New Bitmap(disc_FileName)
Dim L As Integer = CInt(ConfigurationSettings.AppSettings("TNWidth"))
Dim M As Integer = CInt(ConfigurationSettings.AppSettings("MainWidth"))

'///////////////////////////////////////
'The above come from web.config in the <appSettings> section

' <add key="TNWidth" value="100"/>
' <add key="MainWidth" value="400"/>

'The values are in pixels, so I want my main photo to be 400px wide and scaled in height accordingly. The thumb nail is to be 100px wide
'///////////////////////////////////


Dim sScale As Double = 0
Dim sScale2 As Double = 0
Dim M2 As Integer = M / 2
Dim L2 As Integer = L / 2

With jPeg
If (.Height < .Width) Then
sScale = L / .Width
sScale2 = M / .Width
Else
sScale = L2 / .Height
sScale2 = M2 / .Height
End If
End With

Dim NewWidth As Integer = CInt(sScale * jPeg.Width)
Dim NewHeight As Integer = CInt(sScale * jPeg.Height)
Dim NewWidth2 As Integer = CInt(sScale2 * jPeg.Width)
Dim NewHeight2 As Integer = CInt(sScale2 * jPeg.Height)

Dim OBM As Bitmap = New Bitmap(jPeg, NewWidth, NewHeight)
Dim OBM2 As Bitmap = New Bitmap(jPeg, NewWidth2, NewHeight2)

OBM.Save(Server.MapPath(".\photos\TN\") & LCase(FileName), ImageFormat.Jpeg)
OBM2.Save(Server.MapPath(".\photos\") & LCase(FileName), ImageFormat.Jpeg)
OBM.Dispose()
OBM2.Dispose()
jPeg.Dispose()

Dim db_FileName As String
Dim db_Thumb As String
Dim bR As Boolean


db_FileName = "http://www.mydomain.com/photos/" & FileName
db_Thumb = "http:///www.mydomain.com/photos/TN/" & FileName


bR = UploadPhotosToDB(CInt(viewstate("MemberID")), db_FileName, db_Thumb) ' a function which adds the members id and names of the uploaded photos to a database

If bR Then
lblFileName.Text = "Upload OK"
Else
lblFileName.Text = "SORRY, UPLOAD FAILURE - PLEASE TRY AGAIN"
End If

Try
'delete tmp photo
Dim objFSO
objFSO = Server.CreateObject("Scripting.FileSystemObject")

disc_FileName = Server.MapPath(".\photos\temp\") & LCase(FileName)

objFSO.DeleteFile(disc_FileName, False)
objFSO = Nothing
Catch ex As Exception
End Try


End Sub

.....later.....
Antic provided this very good link for more about imaging

http://aspnet.4guysfromrolla.com/articles/012203-1.aspx
 
Back
Top