Click here to Skip to main content
15,885,537 members
Articles / Web Development / ASP.NET
Tip/Trick

Resizing images while uploading in ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
15 Jul 2011CPOL 37.9K   16   3
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.


VB
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.

License

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


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

Comments and Discussions

 
GeneralReason for my vote of 5 it` really working Pin
vitrag22-Dec-11 0:24
vitrag22-Dec-11 0:24 
Question[My vote of 1] This is NOT resizing while uploading Pin
37Stars18-Jul-11 13:33
37Stars18-Jul-11 13:33 
AnswerRe: [My vote of 1] This is NOT resizing while uploading Pin
NebojsaPopovic10-Aug-14 0:34
NebojsaPopovic10-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.