Code Simplified – Viral Sarvaiya

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

Posts Tagged ‘image resize’

Image Resize or create thumb image

Posted by Viral Sarvaiya on August 4, 2009

The project shows the resizing of the image, means create the thumb file of the actual image, here i show how to create the thumb as well as Medium sized file.

first create the class file named “GeneralMethods.vb”

Public Class GeneralMethods
Public Shared Function CreateThumbImage(ByVal imgPhoto As Image, ByVal Width As Integer, ByVal Height As Integer) As Image
Dim bmpOut As Bitmap = Nothing
Dim oBMP As New Bitmap(imgPhoto)
Dim oFormat As Imaging.ImageFormat = oBMP.RawFormat
Dim NewWidth As Integer = 0
Dim NewHeight As Integer = 0

'*** If the image is smaller than a thumbnail just return it

If oBMP.Width <= Width AndAlso oBMP.Height <= Height Then
Return imgPhoto
End If

Dim per As Double
If oBMP.Width > Width Or oBMP.Height > Height Then
If oBMP.Width > oBMP.Height Then
per = (100 * Width) / oBMP.Width
NewWidth = (oBMP.Width * per) / 100
NewHeight = (oBMP.Height * per) / 100
per = (100 * Height) / oBMP.Height
NewWidth = (oBMP.Width * per) / 100
NewHeight = (oBMP.Height * per) / 100
End If

If NewHeight > Height Or NewWidth > Width Then
If NewWidth > NewHeight Then
per = (100 * Width) / NewWidth
per = (100 * Height) / NewHeight
End If
NewWidth = (NewWidth * per) / 100
NewHeight = (NewHeight * per) / 100
End If

End If

bmpOut = New Bitmap(NewWidth, NewHeight)

Dim g As Graphics = Graphics.FromImage(bmpOut)

g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
g.InterpolationMode = Drawing2D.InterpolationMode.High
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality

Dim codec As Drawing.Imaging.ImageCodecInfo = Drawing.Imaging.ImageCodecInfo.GetImageEncoders(1)
Dim eParams As Drawing.Imaging.EncoderParameter = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 1)

g.FillRectangle(Brushes.White, 0, 0, NewWidth, NewHeight)

g.DrawImage(oBMP, 0, 0, NewWidth, NewHeight)


Return Nothing
End Try
Return bmpOut
End Function

Imports this 2 namesapces,
Imports System.IO
Imports System.Drawing

in the html side, take a fileupload control named “file1” and one button.
on the click of the button write the following code

If file1.HasFile Then
extension = Path.GetExtension(file1.PostedFile.FileName).ToLower()
SyncLock (objLock)
'get the max id from the database
maxId = (objclass.GetMaxId())
filename = maxId & extension

strImagePath = rootPath & ConfigurationManager.AppSettings("ACTUAL_PATH") & filename
End SyncLock
If Not Directory.Exists(rootPath & ConfigurationManager.AppSettings("ACTUAL_PATH")) Then
Directory.CreateDirectory(rootPath & ConfigurationManager.AppSettings("ACTUAL_PATH"))
End If

objprice.photograph = filename


If File.Exists(strImagePath) Then
Dim actualImage As Drawing.Image
Dim mediumImage As Drawing.Image = Nothing
Dim thumbImage As Drawing.Image = Nothing

actualImage = Drawing.Image.FromFile(strImagePath)

'------------- Medium Image --------------------
Dim mediumWidth As Integer = ConfigurationManager.AppSettings("MEDIUM_WIDTH")
Dim mediumHeight As Integer = ConfigurationManager.AppSettings("MEDIUM_HEIGHT")
mediumImage = GeneralMethods.CreateThumbImage(actualImage, mediumWidth, mediumHeight)

If mediumImage.Width > mediumWidth Then
mediumImage = mediumImage.GetThumbnailImage(mediumWidth, mediumImage.Height, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If mediumImage.Height > mediumHeight Then
mediumImage = mediumImage.GetThumbnailImage(mediumImage.Width, mediumHeight, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If mediumImage IsNot Nothing Then
mediumImage.Save(rootPath & ConfigurationManager.AppSettings("MEDIUM_PATH") & filename)
End If
'-------------  End Medium  --------------------

actualImage = Drawing.Image.FromFile(strImagePath)
'--------------- Thumb Image -----------------
Dim thumbWidth As Integer = ConfigurationManager.AppSettings("THUMB_WIDTH")
Dim thumbHeight As Integer = ConfigurationManager.AppSettings("THUMB_HEIGHT")
thumbImage = GeneralMethods.CreateThumbImage(actualImage, thumbWidth, thumbHeight)

If thumbImage.Width > thumbWidth Then
thumbImage = thumbImage.GetThumbnailImage(thumbWidth, thumbImage.Height, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If thumbImage.Height > thumbHeight Then
thumbImage = thumbImage.GetThumbnailImage(thumbImage.Width, thumbHeight, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If thumbImage IsNot Nothing Then
thumbImage.Save(rootPath & ConfigurationManager.AppSettings("THUMB_PATH") & filename)
End If
'------------- End Thumb Image -----------------

End If

End If
Private Function ThumbnailCallback() As Boolean
Return False
End Function

according to the requirements set the application setting of the web.config.

hope this will help you

enjoy coding…..


Posted in ASP.NET | Tagged: , , , | Leave a Comment »

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.

Posted in ASP.NET | Tagged: , , , , | Leave a Comment »

%d bloggers like this: