Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider
Using ExImageResizer

Recently I published my .NET class to resize images on Code Plex. I wanted to follow up with a demonstration of how the class is used.

First you will need to compile the class library and then add a reference to the resulting dll to either a web project, smart client, etc. This will give you access to the class' functionality. Add a FileUpload control, a Submit Button and an Image control. Add a click event handler for the submit button.

Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    If FileUpload1.HasFile Then

        Dim ir As New ExImage

        ir.ResizeFromStream(Path.Combine( _
            HttpContext.Current.Request.PhysicalApplicationPath, _
            Path.GetFileName(FileUpload1.FileName)), 200, 200, _
                FileUpload1.PostedFile.InputStream)
        Image1.ImageUrl = Path.Combine(WebRoot, FileUpload1.FileName)
        Image1.Visible = True

    End If
End Sub

In the click event handler you can store a resized image with one line of code. Pass the destination image name and full path, the dimensions of the image and the stream from the File Upload control. The ResizeFromStream method will create a new smaller image that will not exceed the dimensions passed to it. It is smart enough to honor the best fit principle where the width is more important than the height. I chose this way because with the web the image width is the higher priority than an images height, because people will scroll vertically, but hate to scroll horizontally.

There are of course many other overloads to the method so you can resize images on the file system. You can also specify the quality of the resize job, etc. I am excited about this and I hope to get back some great feedback from developers using the class.

I just added a sample web site to the source for the project. I even checked it out and made sure it works too!!!

Share this post :

kick it on DotNetKicks.com
Posted: Saturday, February 02, 2008 5:17 PM

by Chris Love
Filed under: , ,

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 5, 2008 9:43 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS