gogetsome
asked on
Uploaded picture sometimes blurry
Hello, I need some advise and direction on my code below or a better way to accomplish my goal of allowing a user to upload an image to the server. When the image is uploaded I need to create a thumbnail and ensure the image is no wider than 400px. I'm using the code below which functions correctly for me but some users get blurry results for the image and thumb. I use photoshop for my picture adjustment and have no idea what others use. I have them convert their pictures to 400px width before uploading and still they sometimes come out blurry. I take the original picture and adjust in photoshop and bam it looks great!
Any advice or tweeks to my code would be greatly apreciated!
Here is what I'm working with:
Private Sub InsertPicture1()
'Insert imageurl and username to db
If fuPicture1.HasFile Then
If ddlEventGroup.Visible = True Then
Dim EventGroup As String = ddlEventGroup.Text.Trim
Dim ImageUrL As String = (EventGroup + "_" + fuPicture1.FileName)
Dim myConnection As OleDbConnection = New OleDbConnection("Provider= Microsoft. Jet.OLEDB. 4.0; data source=" & Server.MapPath("~/App_Data /LandT.mdb "))
Dim ad As OleDbDataAdapter = New OleDbDataAdapter("insert into EventPhotos ([EventGroup], [ImageURL]) values ('" & Replace(EventGroup, "'", "''") & "', '" & Replace(ImageUrL, "'", "''") & "')", myConnection)
Dim ds As DataSet = New DataSet()
ad.Fill(ds)
'upload the picture
Dim fileName As String = Server.HtmlEncode(fuPictur e1.FileNam e)
Dim extension As String = System.IO.Path.GetExtensio n(fileName )
Dim newFileName As String = ddlEventGroup.Text + "_" + fileName
Dim Path As String = Server.MapPath("~/EventPic tures/" & newFileName)
If (extension = ".jpeg") Or (extension = ".jpg") Or (extension = ".gif") Or (extension = ".JPEG") Or (extension = ".JPG") Or (extension = ".GIF") Then
fuPicture1.SaveAs(Path)
CreateImage(Path, Server.MapPath("~/EventPic tures/Imag e/" & newFileName))
CreateThumbnail(Path, Server.MapPath("~/EventPic tures/Thum b/" & newFileName))
lblErrorMessage.Text = ""
Else
lblErrorMessage.Text = "Sorry, but the picture must be a .jpg, .gif"
End If
'delete original picture
Dim PictueName As String = (ddlEventGroup.Text + "_" + fuPicture1.FileName)
Dim filePath As String = ("~/EventPictures/" + PictueName)
File.Delete(Server.MapPath (Trim(file Path)))
Else
If txtEventGroup.Visible = True Then
Dim EventGroup As String = txtEventGroup.Text.Trim
Dim ImageUrL As String = (EventGroup + "_" + fuPicture1.FileName)
Dim myConnection As OleDbConnection = New OleDbConnection("Provider= Microsoft. Jet.OLEDB. 4.0; data source=" & Server.MapPath("~/App_Data /LandT.mdb "))
Dim ad As OleDbDataAdapter = New OleDbDataAdapter("insert into EventPhotos ([EventGroup], [ImageURL]) values ('" & Replace(EventGroup, "'", "''") & "', '" & Replace(ImageUrL, "'", "''") & "')", myConnection)
Dim ds As DataSet = New DataSet()
ad.Fill(ds)
'upload the picture
Dim fileName As String = Server.HtmlEncode(fuPictur e1.FileNam e)
Dim extension As String = System.IO.Path.GetExtensio n(fileName )
Dim newFileName As String = txtEventGroup.Text + "_" + fileName
Dim Path As String = Server.MapPath("~/EventPic tures/" & newFileName)
If (extension = ".jpeg") Or (extension = ".jpg") Or (extension = ".gif") Or (extension = ".JPEG") Or (extension = ".JPG") Or (extension = ".GIF") Then
fuPicture1.SaveAs(Path)
CreateImage(Path, Server.MapPath("~/EventPic tures/Imag e/" & newFileName))
CreateThumbnail(Path, Server.MapPath("~/EventPic tures/Thum b/" & newFileName))
lblErrorMessage.Text = ""
Else
lblErrorMessage.Text = "Sorry, but the picture must be a .jpg, or .gif"
End If
'delete original picture
Dim PictueName As String = (txtEventGroup.Text + "_" + fuPicture1.FileName)
Dim filePath As String = ("~/EventPictures/" + PictueName)
File.Delete(Server.MapPath (Trim(file Path)))
Else
End If
End If
End If
End Sub
Public Sub CreateImage(ByVal srcpath As String, ByVal destpath As String)
Dim img As System.Drawing.Image = System.Drawing.Image.FromF ile(srcpat h)
Dim imgRatio As Double = img.Width / img.Height
Dim tnWidth As Double = 400
Dim tnHeight As Double = 400
If imgRatio > 1 Then
tnHeight = (400 * img.Height) / img.Width
Else
tnWidth = (400 * img.Width) / img.Height
End If
Dim Image As System.Drawing.Image = img.GetThumbnailImage(tnWi dth, tnHeight, Nothing, New System.IntPtr(0))
Image.Save(destpath, ImageFormat.Jpeg)
img.Dispose()
Image.Dispose()
End Sub
Public Sub CreateThumbnail(ByVal srcpath As String, ByVal destpath As String)
Dim img As System.Drawing.Image = System.Drawing.Image.FromF ile(srcpat h)
Dim imgRatio As Double = img.Width / img.Height
Dim tnWidth As Double = 100
Dim tnHeight As Double = 100
If imgRatio > 1 Then
tnHeight = (100 * img.Height) / img.Width
Else
tnWidth = (100 * img.Width) / img.Height
End If
Dim Thumb As System.Drawing.Image = img.GetThumbnailImage(tnWi dth, tnHeight, Nothing, New System.IntPtr(0))
Thumb.Save(destpath, ImageFormat.Jpeg)
img.Dispose()
Thumb.Dispose()
End Sub
Any advice or tweeks to my code would be greatly apreciated!
Here is what I'm working with:
Private Sub InsertPicture1()
'Insert imageurl and username to db
If fuPicture1.HasFile Then
If ddlEventGroup.Visible = True Then
Dim EventGroup As String = ddlEventGroup.Text.Trim
Dim ImageUrL As String = (EventGroup + "_" + fuPicture1.FileName)
Dim myConnection As OleDbConnection = New OleDbConnection("Provider=
Dim ad As OleDbDataAdapter = New OleDbDataAdapter("insert into EventPhotos ([EventGroup], [ImageURL]) values ('" & Replace(EventGroup, "'", "''") & "', '" & Replace(ImageUrL, "'", "''") & "')", myConnection)
Dim ds As DataSet = New DataSet()
ad.Fill(ds)
'upload the picture
Dim fileName As String = Server.HtmlEncode(fuPictur
Dim extension As String = System.IO.Path.GetExtensio
Dim newFileName As String = ddlEventGroup.Text + "_" + fileName
Dim Path As String = Server.MapPath("~/EventPic
If (extension = ".jpeg") Or (extension = ".jpg") Or (extension = ".gif") Or (extension = ".JPEG") Or (extension = ".JPG") Or (extension = ".GIF") Then
fuPicture1.SaveAs(Path)
CreateImage(Path, Server.MapPath("~/EventPic
CreateThumbnail(Path, Server.MapPath("~/EventPic
lblErrorMessage.Text = ""
Else
lblErrorMessage.Text = "Sorry, but the picture must be a .jpg, .gif"
End If
'delete original picture
Dim PictueName As String = (ddlEventGroup.Text + "_" + fuPicture1.FileName)
Dim filePath As String = ("~/EventPictures/" + PictueName)
File.Delete(Server.MapPath
Else
If txtEventGroup.Visible = True Then
Dim EventGroup As String = txtEventGroup.Text.Trim
Dim ImageUrL As String = (EventGroup + "_" + fuPicture1.FileName)
Dim myConnection As OleDbConnection = New OleDbConnection("Provider=
Dim ad As OleDbDataAdapter = New OleDbDataAdapter("insert into EventPhotos ([EventGroup], [ImageURL]) values ('" & Replace(EventGroup, "'", "''") & "', '" & Replace(ImageUrL, "'", "''") & "')", myConnection)
Dim ds As DataSet = New DataSet()
ad.Fill(ds)
'upload the picture
Dim fileName As String = Server.HtmlEncode(fuPictur
Dim extension As String = System.IO.Path.GetExtensio
Dim newFileName As String = txtEventGroup.Text + "_" + fileName
Dim Path As String = Server.MapPath("~/EventPic
If (extension = ".jpeg") Or (extension = ".jpg") Or (extension = ".gif") Or (extension = ".JPEG") Or (extension = ".JPG") Or (extension = ".GIF") Then
fuPicture1.SaveAs(Path)
CreateImage(Path, Server.MapPath("~/EventPic
CreateThumbnail(Path, Server.MapPath("~/EventPic
lblErrorMessage.Text = ""
Else
lblErrorMessage.Text = "Sorry, but the picture must be a .jpg, or .gif"
End If
'delete original picture
Dim PictueName As String = (txtEventGroup.Text + "_" + fuPicture1.FileName)
Dim filePath As String = ("~/EventPictures/" + PictueName)
File.Delete(Server.MapPath
Else
End If
End If
End If
End Sub
Public Sub CreateImage(ByVal srcpath As String, ByVal destpath As String)
Dim img As System.Drawing.Image = System.Drawing.Image.FromF
Dim imgRatio As Double = img.Width / img.Height
Dim tnWidth As Double = 400
Dim tnHeight As Double = 400
If imgRatio > 1 Then
tnHeight = (400 * img.Height) / img.Width
Else
tnWidth = (400 * img.Width) / img.Height
End If
Dim Image As System.Drawing.Image = img.GetThumbnailImage(tnWi
Image.Save(destpath, ImageFormat.Jpeg)
img.Dispose()
Image.Dispose()
End Sub
Public Sub CreateThumbnail(ByVal srcpath As String, ByVal destpath As String)
Dim img As System.Drawing.Image = System.Drawing.Image.FromF
Dim imgRatio As Double = img.Width / img.Height
Dim tnWidth As Double = 100
Dim tnHeight As Double = 100
If imgRatio > 1 Then
tnHeight = (100 * img.Height) / img.Width
Else
tnWidth = (100 * img.Width) / img.Height
End If
Dim Thumb As System.Drawing.Image = img.GetThumbnailImage(tnWi
Thumb.Save(destpath, ImageFormat.Jpeg)
img.Dispose()
Thumb.Dispose()
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER