Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Image Upload and Resize

Posted by Viral Sarvaiya on March 13, 2009

Lets assume we have a FileUpload control (named File1) and a Button control ( imgExtSubmit) and we’re going to upload the image when we click our button. The simplest method of uploading our file might look something like this :

for this you have to add the Namespace :

Imports System.Drawing
Imports System.Drawing.Imaging

Protected Sub imgExtSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgExtSubmit.Click

If File1.HasFile Then

Dim file As String = System.DateTime.Now.Millisecond

If File1.FileName <> “” Then
imgName = GeneralMethods.GetCallbackPath(File1.FileName, file)
File1.SaveAs(Server.MapPath(“../images/”) & imgName)
GeneralMethods.Thmbimages(Server.MapPath(“../images/”) & imgName, Server.MapPath(“../thumb/”), imgName, 116, 76)
imgName = “noimages.gif”
End If

End If

after this coding make a public class named : GeneralMethods.vb

Public Class GeneralMethods

Public Shared Function GetCallbackPath(ByVal returnValue As String, ByVal Fileno As String) As String
Dim fileName As String = System.IO.Path.GetFileName(returnValue)
Dim strfile() As String = fileName.Split(“.”)
fileName = strfile(0) & Fileno & “.” & strfile(1)
If fileName.Length > 10 Then
Dim i As Integer = CInt(fileName.Length) – 10
fileName = fileName.Substring(i, 10)
i = fileName.Length
End If
Return fileName
End Function

Public Shared Function Thmbimages(ByVal MainPath As String, ByVal ThmbPath As String, ByVal Filename As String, ByVal Passwidth As String, ByVal Passheight As String, Optional ByVal FixFlag As Integer = 0) As String
Dim width, height As Integer
Dim pathname As String
Dim inp As New IntPtr
Dim orginalimg, thumb As System.Drawing.Image

orginalimg = orginalimg.FromFile(MainPath)
width = orginalimg.Width
height = orginalimg.Height

If FixFlag <> 0 Then
width = Passwidth
height = Passheight
If width > Passwidth Or height > Passheight Then
Dim per As Double
If width > height Then
per = (100 * Passwidth) / width
width = (width * per) / 100
height = (height * per) / 100
per = (100 * Passheight) / height
width = (width * per) / 100
height = (height * per) / 100
End If
End If
End If
thumb = orginalimg.GetThumbnailImage(width, height, Nothing, inp)

pathname = ThmbPath & Filename

If File.Exists(pathname) Then
End If

Return Filename
End Function

End Class

this function directly save the actual file to the image folder and make a small image and put it to the THUMB folder.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: