Click here to Skip to main content
Licence CPOL
First Posted 25 Nov 2008
Views 10,522
Bookmarked 10 times

Image resizing with asp.net

By | 25 Nov 2008 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Haresh Chaudhari

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSee imageresizing.net instead PinmemberNathanael Jones6:37 6 Jan '12  
GeneralMy vote of 1 Pinmemberaa.azizkhani19:45 25 Nov '08  
GeneralMy vote of 1 PinmemberJames Luterek5:23 25 Nov '08  
GeneralMy vote of 1 PinmemberRamon Smits4:04 25 Nov '08  
GeneralCaching PinmemberRamon Smits4:03 25 Nov '08  
GeneralBad code PinmemberRamon Smits4:02 25 Nov '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 25 Nov 2008
Article Copyright 2008 by Haresh Chaudhari
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid