Code to Resize Image in ASP.Net

Code to Resize Image in ASP.Net>



Discription


This code is use to convert an Images with different sizes.
sometimes it needs to create one image in number of different sizes/resolutions to upload.
It requires more time.

Imports namespace




System.Drawing
System.Drawing.Graphics


This code will help to create different images from one in different resolutions without disturbing the clearity.
Below is the code to resize an Image of any format in asp.net.


'Mention the Width of image you have to generate
Dim width = "Width of Destination Image"
'Mention the height of image you have to generate
Dim height = "Height of Destination Image"
'operator to seperate width and height
Dim mult = "x"

Dim ImageName = "Enter Name Of Image"
'mention the format of destination image ie. .jpg,.gif,bmp etc
Dim format = "File Format"
'mention the path Where source image is located
Dim src As String = "Source Path (Where image is located)"
'Response.Write("src=" & src)
'mention the path Where destination image is located
Dim dest As String = "Destination Path (Where u want to save new image"
'Response.Write("dest=" & dest)
Dim thumbWidth As Integer = width
Dim thumbHeight As Integer = height

'Convertion code is starts here
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(src)
Dim srcWidth As Integer = image.Width
Dim srcHeight As Integer = image.Height

Dim sizeRatio As Decimal = (CDec(srcHeight) / srcWidth)
'int thumbHeight=Decimal.ToInt32(sizeRatio*thumbWidth);
Dim bmp As New Bitmap(thumbWidth, thumbHeight)


Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
Dim rectDestination As New System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight)
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, _
GraphicsUnit.Pixel)
bmp.Save(dest)
bmp.Dispose()
image.Dispose()
'Convertion code is ends here

MessageBox.Show("Resize Image stored at" & dest)



Thanks
Nilesh Jadhav


Comments

Author: ABitSmart30 Nov 2009 Member Level: Gold   Points : 0

Please format your resource.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: