In this article i discuss about to resize an image on file
upload control..
Normally if the image has more size it will occupy more
bytes,so better we have to resize the image for our appropriate needs..
Even if you would like to show image as thumbnail,in such
case its better resize to small size
then only images looks good.
In an previous post I discuss about to validate the uploaded
images and save the images indatabase,now there is no need to describe it,so we
I share the code directly to resize the image.
Below just pass the fileupload control to the fuction..
This the simple code to resize the image ..
Function resize(ByRef flp As FileUpload)
'*****************www.dotnetcode.in************
Dim height As String = "62"
Dim width As String = "105"
If flp.HasFile Then
Dim myimg As System.Drawing.Image
myimg =
System.Drawing.Image.FromStream(flp.PostedFile.InputStream)
myimg = myimg.GetThumbnailImage("105", "62", Nothing, IntPtr.Zero)
Dim str As New MemoryStream
myimg.Save(str,
System.Drawing.Imaging.ImageFormat.Jpeg)
Dim conbyt As Byte() = New Byte(str.Length -
1) {}
str.Position = 0
str.Read(conbyt, 0,
conbyt.Length)
Return conbyt
End If
End Function
|
Finally we return the images as byte, so we can easily save
the bytes into Database.
In case if you want save into local path ..
myimg.Save("C:\Users\admin\Pictures\car\1.jpg",
myimg.RawFormat)
IF you add some graphics to the image then you have to
follow the below code.
Dim bmpOut As New System.Drawing.Bitmap(NewWidth, NewHeight)
Dim g As System.Drawing.Graphics =
System.Drawing.Graphics.FromImage(bmpOut)
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.FillRectangle(System.Drawing.Brushes.White,
0, 0, NewWidth, NewHeight)
g.DrawImage(New System.Drawing.Bitmap(flp.PostedFile.InputStream), 0,
0, NewWidth, NewHeight)
Dim str As New MemoryStream()
myimg.Save(str,
System.Drawing.Imaging.ImageFormat.Jpeg)
|
Then the resize image has resized with better quality.
I hope you like this article…..keep reading
0 comments:
Post a Comment