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!!!