Image manipulation in ASP.NET: Loading & Resizing
page 1 of 1
Published: 24 Mar 2004
Unedited - Community Contributed
Abstract
This 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.
by Chris Garrett
Feedback
Average Rating: 
Views (Total / Last 10 Days): 60729/ 43

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
/chrisg/tools/view-image2.asp

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.



User Comments

Title: Good job   
Name: zahid
Date: 2009-05-31 1:05:05 AM
Comment:
It's really a good example.
Title: dynamic imagebutton to fetch the image form the database   
Name: aashish
Date: 2009-02-11 12:53:16 AM
Comment:
Dear,

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: Image resizing module   
Name: Nathanael Jones
Date: 2008-08-06 4:23:46 PM
Comment:
Here is a full-fledged image resizing module for ASP.NET

http://nathanaeljones.com/products/asp-net-image-resizer/

It includes managed disk caching to allow use in the 'real world'.
Title: Create Dynamic Image GridView   
Name: Shadi Al-Hourani
Date: 2008-03-31 4:09:55 AM
Comment:
Dear Sir,
I want to develop web page that can dynamic select image from file and display them, Sence loading image all of image loading should display at the same time.
best regard.
Title: Creating Thumbnail   
Name: Gayathri
Date: 2008-02-16 2:11:59 AM
Comment:
Hi
Really an excellent article. Just a few lines to create a thumbnail!! that 2 without any image distortion.

Many Tanx
Title: dynamic image resizing   
Name: hiren
Date: 2007-12-10 9:36:40 PM
Comment:
hi,

do you happen to have similer code in asp.net?
Title: inserting image   
Name: ahmad
Date: 2007-01-09 4:14:35 AM
Comment:
is there no error when i insert image as binary from my client to the server host?
Title: Simple howto create thumbnails   
Name: Simple howto create thumbnails
Date: 2006-10-26 2:23:08 PM
Comment:
This link shows howto create thumbnails based on image files on server:

http://www.dotnetspace.com/index.php?option=com_content&task=view&id=22&Itemid=26
Title: image resizing   
Name: gauravKanwar
Date: 2006-04-07 3:46:52 PM
Comment:
sir when ever i nicrese the size of my picture the image gets distorted can u help me in producing large images
without distortion
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.asp
what is "src" in the code?

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

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-10-19 9:29:19 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search