I made a mix between your code and code of this
thread ;)
Markup :
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default2.aspx.vb" Inherits="MyTestProject.Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<style type="text/css">
#profile_pic_wrapper{ position:relative; border:#ccc solid 1px; width:200px; height:200px;}
#profile_pic_wrapper a{ position:absolute; display:none; top:0; right:0; line-height:20px; padding:5px; color:#fff; background-color:#333; text-decoration:underline;}
#profile_pic_wrapper:hover a{ display:block; }
#profile_pic_wrapper:hover a:hover{text-decoration:none;}
.profile_pic{ width:200px; height:200px;}
</style>
<script type="text/javascript" language="javascript">
function Func() {
document.getElementById("filUpload").click();
document.getElementById("imgProfilePic");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="profile_pic_wrapper">
<asp:Image ID="imgProfilePic" runat="server" ImageUrl="~/cute_girl_001facebook_timeline_cover.jpg" CssClass="profile_pic"/>
<label onclick="Func()">Change picture</label>
<asp:Button runat="server" ID="btnUpload" />
<input type="file" id="filUpload" style="visibility:hidden" runat="server" accept="image/*"/>
<asp:Label runat="server" ID="lblOutput"></asp:Label>
</div>
</div>
</form>
</body>
</html>
Code behind :
Imports System.Drawing
Partial Public Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Dim sSavePath As String
Dim sThumbExtension As String
Dim intThumbWidth As Integer
Dim intThumbHeight As Integer
sSavePath = "images/"
sThumbExtension = "_thumb"
intThumbWidth = 160
intThumbHeight = 120
If filUpload.PostedFile IsNot Nothing Then
Dim myFile As HttpPostedFile = filUpload.PostedFile
Dim nFileLen As Integer = myFile.ContentLength
If (nFileLen = 0) Then
lblOutput.Text = "No file was uploaded."
Return
End If
If Not System.IO.Path.GetExtension(myFile.FileName).ToLower() = ".jpg" Then
lblOutput.Text = "The file must have an extension of JPG"
Return
End If
Dim myData(nFileLen) As Byte
myFile.InputStream.Read(myData, 0, nFileLen)
Dim sFilename As String = System.IO.Path.GetFileName(myFile.FileName)
Dim file_append As Integer = 0
While (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
file_append += 1
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) _
+ file_append.ToString() + ".jpg"
End While
Dim newFile As System.IO.FileStream = New System.IO.FileStream(Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create)
newFile.Write(myData, 0, myData.Length)
newFile.Close()
Dim myCallBack As Image.GetThumbnailImageAbort = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
Dim myBitmap As Bitmap
Try
myBitmap = New Bitmap(Server.MapPath(sSavePath + sFilename))
file_append = 0
Dim sThumbFile As String = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) _
+ sThumbExtension + ".jpg"
While (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile)))
file_append += 1
sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) _
+ file_append.ToString() + sThumbExtension + ".jpg"
End While
Dim myThumbnail As Image = myBitmap.GetThumbnailImage(intThumbWidth, intThumbHeight, myCallBack, IntPtr.Zero)
myThumbnail.Save(Server.MapPath(sSavePath + sThumbFile))
imgProfilePic.ImageUrl = sSavePath + sThumbFile
lblOutput.Text = "File uploaded successfully!"
myThumbnail.Dispose()
myBitmap.Dispose()
Catch errArgument As ArgumentException
lblOutput.Text = "The file wasn't a valid jpg file."
System.IO.File.Delete(Server.MapPath(sSavePath + sFilename))
End Try
End If
End Sub
Public Function ThumbnailCallback() As Boolean
Return False
End Function
End Class