AbstractThis article shows how to load an 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.

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.

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.

I want to fetch all the images from SQL SERVER 2005 and wants to display on dynamic imagebuttons at the page load event.Creation of all the imagebutton should be dynamically accordingly the no. of images in database.

Title:
Question
Name:
Eran Nachum
Date:
2006-02-09 8:48:21 AM
Comment: Hello, nice exmaple!I want to know how can I using this example to render the resized image into an Image (asp:Image) on HTML page.

Regards,Eran Nachum

Title:
developer
Name:
Mohit
Date:
2006-01-18 10:10:04 AM
Comment: Hi I am using ur code to resize images but writing into a binstream instead of to the screen and get a parameter null error ir no image if I use memory stream.....

CAn you help me out....

Title:
developer
Name:
David
Date:
2005-05-10 3:37:06 PM
Comment: In your sample code at http://authors.aspalliance.com/chrisg/tools/view-image2.aspwhat is "src" in the code?

Is it a predefined form field?What does it look like for the request form?