Markup Page
<div id="FileUpd1" runat="server" >
Default: <asp:FileUpload ID="ImgUpload1" runat="server" />
<asp:Button ID="Btn1" runat="server" Text="Submit "
CssClass="btnSubmit" CausesValidation="true"/>
Code Behind - Click to read more...
Imports System.IO
Imports System.Web.UI
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Imaging
Protected Sub Btn1_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Btn1.Click
'define variable
Dim adPhotoName As String = "PhotoNum"
'img directory - physical path
Dim saveDir As String = "\photo_repository\"
Dim appPath As String = Request.PhysicalApplicationPath
'img extensiong check
Dim ImgExt1 As String = LCase(System.IO.Path.GetExtension(ImgUpload1.FileName))
Dim ImgName1 = adPhotoName & "_1" & ImgExt1
If (ImgUpload1.HasFile) Then
'save with custom file name
Dim savePath As String = Path.Combine(Server.MapPath("~/photo_repository"), ImgName1)
'save with default file name
'Dim savePath As String = appPath + saveDir + Server.HtmlEncode(ImgUpload1.FileName)
'allow only jpg and gif photo files
If (ImgExt1.ToString = ".jpg") Or
(ImgExt1.ToString = ".jpeg") Or
(ImgExt1.ToString = ".gif") Then
'resize image before upload
Dim image As New Bitmap(System.Drawing.Image.FromStream +
(ImgUpload1.PostedFile.InputStream))
Dim width As Integer = 448 'image width
Dim height As Integer = 336 'image height
Using thumbnail As System.Drawing.Image = image.GetThumbnailImage +
(width, height, New System.Drawing.Image.GetThumbnailImageAbort +
(AddressOf ThumbnailCallback), IntPtr.Zero)
Using memoryStream As New MemoryStream()
'**default img name - use these two lines of code
'thumbnail.Save(Server.MapPath("~/Uploaded/") +
'Path.GetFileName(Me.ImgUpload1.FileName), ImageFormat.Png)
'********
thumbnail.Save(savePath)
End Using
End Using
'ends here
'confirm image upload
ImgUpConf1 = ImgName1.ToString
FileUpd1.InnerHtml = +
"<table><tr><td><img src='../photo_repository/" +
ImgUpConf1 & "' height='35' width='50'>" +
"<td style='vertical-align: top;'><strong>Image 1: " +
ImgUpload1.FileName & "<br />" +
"<span class='lblAdded'>Image 1 successfuly uploaded!"
Else
'alert if img fiel ext <> jpg or gif
BadImgFormat = ImgExt1.ToString
ImgName1 = NoImage 'db photo record
FileUpd1.InnerHtml = +
"<table><tr><td><img src='../photo_repository/no_image.jpg' " +
"height='35' width='50'/>" +
"<td style='vertical-align: top;' ><span style='color: red;'>" +
"ERROR: The default image format is " & BadImgFormat & "!<br />" +
"Only .jpg and .gif file type allowed.<br /><a href='ad_photo.aspx?img=1'" +
"class='colorbox2'>Try again."
End If
End Sub
'define function
Public Function ThumbnailCallback() As Boolean
Return False
End Function
End Class