Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Image resizing with asp.net

0.00/5 (No votes)
23 Nov 2008 1  
Resizing the very large size images with help of asp.net and vb.net


ResizeImages.JPG

 

Introduction  

Sometime when we are uploading images and if we resize with applying variable size then images are not displayed in good quality. So, this article show how to resize images without scratch the image.

Background 

In this article we have to use some drawing methods.
 

Using the code 

' Create new stream.
' Dim stream As New FileStream(fileName, FileMode.Open, FileAccess.Read)

' Create new image.
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(Stream)

' Calculate proportional max width and height.
Dim oldWidth As Integer = image.Width
Dim oldHeight As Integer = image.Height
If (CDec(oldWidth) / CDec(oldHeight)) > (CDec(newWidth) / CDec(newHeight)) Then
    Dim ratio As Decimal = CDec(newWidth) / oldWidth
    newHeight = CInt((oldHeight * ratio))
Else
    Dim ratio As Decimal = CDec(newHeight) / oldHeight
    newWidth = CInt((oldWidth * ratio))
End If

' Create a new bitmap with the same resolution as the original image.
Dim bitmap As New Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb)
bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution)

' Create a new graphic.
Dim graphics__1 As Graphics = Graphics.FromImage(bitmap)
graphics__1.Clear(Color.White)
graphics__1.InterpolationMode = InterpolationMode.HighQualityBicubic

' Create a scaled image based on the original.
graphics__1.DrawImage(image, New Rectangle(0, 0, newWidth, newHeight),
                      New Rectangle(0, 0, oldWidth, oldHeight), GraphicsUnit.Pixel)
                      graphics__1.Dispose()

 ' Save the scaled image.
 bitmap.Save(Server.MapPath("Images/") & strDestinationFileName, image.RawFormat)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here