Click here to Skip to main content
13,005,644 members (76,553 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


15 bookmarked
Posted 15 Jul 2011

Resizing images while uploading in ASP.NET

, 15 Jul 2011
Rate this:
Please Sign up or sign in to vote.
A method for resizing images automatically when they are being uploaded.

Tired of big sized images uploaded on your website? Well, this is a way to resize images automatically in the code when they are being uploaded, to make their size smaller.

Imports System.Drawing
Imports System.Drawing.Drawing2D
Dim gu As New Guid
gu = Guid.NewGuid
Dim ext As String = Path.GetExtension(FileUpload1.FileName)
ImageName = gu.ToString & ext
FileUpload1.SaveAs(Server.MapPath("~/DynamicImages/News/" & ImageName))
Using fl = File.OpenRead(Server.MapPath("~/DynamicImages/News/" & ImageName))
    Using thumbFile = File.Create(Server.MapPath("~/DynamicImages/News/thumbnails/" & ImageName))
        ResizeImage(0.1, fl, thumbFile)
    End Using
End Using
Private Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream As Stream)
    Using image__1 = Image.FromStream(fromStream)
        'Dim newWidth = CInt(image__1.Width * scaleFactor)
        ' Dim newHeight = CInt(image__1.Height * scaleFactor)
        Dim newWidth = 150
        Dim newHeight = 150
        Using thumbnailBitmap = New Bitmap(newWidth, newHeight)
            Using thumbnailGraph = Graphics.FromImage(thumbnailBitmap)
                thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality
                thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality
                thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic
                Dim imageRectangle = New Rectangle(0, 0, newWidth, newHeight)
                thumbnailGraph.DrawImage(image__1, imageRectangle)
                thumbnailBitmap.Save(toStream, image__1.RawFormat)
            End Using
        End Using
    End Using
End Sub

The thing here is in the resize image function, which makes the size of your images smaller. Note that I am storing the small images in a folder named ~/DynamicImages/News/thumbnails/, and I am adding to the images a unique ID using the GUID.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

mhamad zarif
Software Developer Netiks
Lebanon Lebanon
I have been a programmer for about one year.My power is in and flash (actionscript 3).I also work on windows application using and c#.

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 it` really working Pin
vitrag22-Dec-11 0:24
membervitrag22-Dec-11 0:24 
Question[My vote of 1] This is NOT resizing while uploading Pin
37Stars18-Jul-11 13:33
member37Stars18-Jul-11 13:33 
AnswerRe: [My vote of 1] This is NOT resizing while uploading Pin
NebojsaPopovic10-Aug-14 0:34
memberNebojsaPopovic10-Aug-14 0:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 15 Jul 2011
Article Copyright 2011 by mhamad zarif
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid