LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Image manipulation in ASP.NET: Loading & Resizing
by Chris Garrett
Average Rating: 
Views (Total / Last 10 Days): 16199/ 20

Image manipulation in ASP.NET: Loading & Resizing

In the previous article I showed you how to dynamically create an image on the fly. In this and the next article we will be loading existing images and creating thumbnails.

This article shows how to load and image from your server and return it to the browser using the correct mime type by detecting the image format of the loaded file. We will also resize the image making a simplistic thumbnail.


The Code

View the source of this project here

I have broken the project into two functions, one is our routine and the other returns an error. The error routine uses the string code from the previous article so we will concentrate instead on "sendFile()".

SendFile creates two objects, the first is system.drawing.image which we create using an image with the "fromFile" constructor. This loads in a file using the supplied filename (made into a proper filename from a web relative path using mappath) and returns an Image.

The next object is a bitmap created using the above image, with the width and height set to whatever is supplied on the URL.

Now we have a working image that is basically a copy of the file on the hard disk, and a new copy that is resized.

Content Type

Before we can output the image we need to determine if it is a .gif or a .jpg so the end users browser does not throw up a download box.

We check the format by comparing using ".Equals" against a known format (system.drawing.imaging.imageformat.Gif).

Once we know what format the image is we can output the response.contenttype appropriately which will tell the browser what image file format we are sending.

To finish off we simply use the bitmaps save method to send to the user's response stream.

Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-02-27 7:35:04 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search