KuljeetSingh.com

Oct 16, 2012

Image Uploading and Resizing in ASP.NET

We often give Image uploading facility to our site users where they can upload image of their own / products etc. Users are free to upload any size of image, however we can control the size of image for neat and fast display of image after uploading. Here I discuss the way to resize the image in ASP.NET before saving it to server. The method can also be used to create thumbnails in ASP.NET

Steps:
1. A new bitmap image is created with the desired height and width Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(widthInPixels, heightInPixels)
2. Creating and using the graphics object from new image Using graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap)
.......
.......
End Using

3. Using DrawImage function to create image from the original image / converting image from input stream of file upload control graphic.DrawImage(originalImg, 0, 0, widthInPixels, heightInPixels)
4. Interpolation Method and SmoothingMode used to upload in required quality of image
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality

5. New image is returned and saved in to desired folder

Complete Procedure
Using bmp As System.Drawing.Bitmap = ResizeImage(New System.Drawing.Bitmap(FileUpload1.PostedFile.InputStream), 200, 200)
bmp.Save(Server.MapPath("ImagesFolder/") & Path.GetFileName(FileUpload1.FileName))
End Using

Public Function ResizeImage(ByVal originalImg As System.Drawing.Bitmap, ByVal widthInPixels As Integer, ByVal heightInPixels As Integer) As System.Drawing.Bitmap
Dim bitmap As System.Drawing.Bitmap
bitmap = New System.Drawing.Bitmap(widthInPixels, heightInPixels)
Using graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap)
' Quality properties
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
graphic.DrawImage(originalImg, 0, 0, widthInPixels, heightInPixels)
Return bitmap
End Using
End Function

Download Sample Code Here

 
?

Blogs | About Me | My Projects | Contact Me
【丰胸】虽然,这些药物对于乳房而言粉嫩公主,能够起到一定的丰胸效果,可朋友们这些药物的后遗症,也会在我们服完后慢慢显现出来丰胸产品,譬如:机体中的雌激素水平被打破了,就容易导致我们的月经变得不正常丰胸食物,从而使得我们的腰跟着变得圆嘟嘟,而严重的话还可能会导致我们的子宫出血,肝脏受损丰胸效果,所以说若是采用药物来进行丰胸,大家还是小心谨慎些为好。