Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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.

License

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

Share

About the Author

mhamad zarif
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 Pinmembervitrag22-Dec-11 0:24 
Question[My vote of 1] This is NOT resizing while uploading Pinmember37Stars18-Jul-11 13:33 
AnswerRe: [My vote of 1] This is NOT resizing while uploading PinmemberNebojsaPopovic10-Aug-14 0:34 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 15 Jul 2011
Article Copyright 2011 by mhamad zarif
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid