Resize Image and Upload Image to Web Directory and write to DB

Rani

Well-known member
Joined
Nov 19, 2005
Messages
71
Programming Experience
Beginner
Goal is to re-size an Uploaded image to Web Directory and write to DB

Hi,
I have this code. I debug to see where It is not working. I have problem at this location.
Quote:

Dim bitmap As New Drawing.Bitmap(imageToBeResized, imageWidth, imageHeight)
Dim stream As System.IO.MemoryStream = New MemoryStream()
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
stream.Position = 0
Dim data(stream.Length) As Byte
stream.Read(data, 0, stream.Length)
UnQuote:


This is my Entire code: Can someone help me fix that section of code? Thanks.

Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click

Dim ddl As String = Me.ddl.SelectedItem.Value.ToString
Dim txt As String = Me.txtarea.Text
Dim lblowner As String = Session("uid") 'this could be 'Lcase(Replace(Request.ServerVariables("AUTH_USER"),"WLT\",""))

Dim s_folder As String 's_folder
Dim l_folder As String 'l_folder
Dim s_filepath As String ' s_filepath & filename
Dim l_filepath As String ' l_filepath & filename
Dim filename As String
Dim extension As String

'variables to store the path of the folders s_photos and l_photos
s_folder = Request.ServerVariables("appl_physical_path") & "s_photos\"
l_folder = Request.ServerVariables("appl_physical_path") & "l_photos\"

'If the fileupload control fUpload has a file to upload then get the filename
If fUpload.PostedFile IsNot Nothing AndAlso fUpload.PostedFile.FileName <> "" Then
filename = Path.GetFileName(fUpload.PostedFile.FileName)

'variable storing path appended with the filename
s_filepath = s_folder & filename
l_filepath = l_folder & filename

'save the uploaded file to the disk
If File.Exists(s_filepath) And File.Exists(l_filepath) Then
Me.lblconfirm.ForeColor = Drawing.Color.Maroon
Me.lblconfirm.Text = filename & " already exits on the server!"
Else
extension = System.IO.Path.GetExtension(fUpload.FileName)
If extension = ".jpg" Or extension = ".bmp" Or extension = ".gif" Or extension = ".jpeg" Then


' Resize Image
Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(fUpload.PostedFile .InputStream)
Dim imageHeight As Integer = imageToBeResized.Height
Dim imageWidth As Integer = imageToBeResized.Width
Dim maxHeight As Integer = 480
Dim maxWidth As Integer = 640
imageHeight = (imageHeight * maxWidth) / imageWidth 'ht = 240*320/320
imageWidth = maxWidth 'wid = 320

If imageHeight > maxHeight Then
imageWidth = (imageWidth * maxHeight) / imageHeight
imageHeight = maxHeight
Else
End If

Dim bitmap As New Drawing.Bitmap(imageToBeResized, imageWidth, imageHeight)
Dim stream As System.IO.MemoryStream = New MemoryStream()
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
stream.Position = 0
Dim data(stream.Length) As Byte
stream.Read(data, 0, stream.Length)


'update the database fields
Dim sql As String = "Insert into Photo(photocatid,uid,date,filename,descrip,filepat h)values('" & ddl & "','" & lblowner & "','" & Now.Date & "','" & filename & "','" & txt & "','" & l_filepath & "')"
Dim con As New SqlConnection(Application("connection"))
Dim da As New SqlDataAdapter(sql, con)
Dim ds As New Data.DataSet
da.Fill(ds)
con.Close()
da = Nothing
ds = Nothing
con = Nothing
Me.lblconfirm.Visible = "True"
Me.lblconfirm.ForeColor = Drawing.Color.Maroon
Me.lblconfirm.Text = filename & " has been sucessfully uploaded"

'Also save files to disk
'fUpload.SaveAs(Server.MapPath("l_photos/" & strfilename))
fUpload.SaveAs(Request.ServerVariables("appl_physical_path") & "l_photos\" & filename)
fUpload.SaveAs(Request.ServerVariables("appl_physical_path") & "s_photos\" & filename)

Else
Me.lblconfirm.Visible = "True"
Me.lblconfirm.ForeColor = Drawing.Color.Maroon
Me.lblconfirm.Text = "Sorry! You can only upload Image files with extenstions .jpg, .jpeg, .bmp and .gif!"
End If
End If
End If

End Sub
 
Back
Top