Resize Image using asp.net Method - 1

Here We have to discuss, how to dynamically resize the images using asp.net , generally we used two image one as Thumbnail and another one is big image , so if you upload the big image , we can set the default height and width on code to resize that image , at the same while we upload small image , on resizing the image it will be stretched to the specified height and width and also not seen qualify of image is not good,

so here , we calculate the width , as per we resize the image

I would prefer this method.This is the First Method , for this place one fileupload control and button on you design form


Dim thumbWidth As Integer = 132

Dim image As System.Drawing.Image = System.Drawing.Image.FromStream

(FileUpload1.PostedFile.InputStream)
'Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.
Dim srcWidth As Integer = image.Width
Dim srcHeight As Integer = image.Height
Dim thumbHeight As Integer = (srcHeight / srcWidth) * thumbWidth
Dim bmp As New Bitmap(thumbWidth, thumbHeight)
'Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
'Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
'Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
'Set the System.Drawing.Graphics object property InterpolationMode to High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
'Draw the original image into the target Graphics object scaling to the desired width and height
Dim rectDestination As New System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight)
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel)

'Save to destination file
bmp.Save(Server.MapPath("~/images/" & FileUpload1.PostedFile.FileName))

' dispose / release resources
bmp.Dispose()
image.Dispose()

If you have better solution, just tell me !

0 comments: