Genimage. ashx <% @ Webhandler Language="C #"Class="Netpix. imagegenerator" %>
Genimage. ashx. CS// Copyright (c) 2003 by Greg Ennis
// (Mailto: greg@ennis.net)
//
// The contents of this file are subject to the artistic license (the "License ").
// You may not use this file before t in compliance with the license.
// You may obtain a copy of the license:
// Http://www.opensource.org/licenses/artistic-license.html
Using System;
Using System. collections;
Using System. componentmodel;
Using System. Data;
Using System. Data. sqlclient;
Using System. drawing;
Using System. Web;
Using System. IO;
Using System. configuration;
Using System. Web. sessionstate;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. htmlcontrols;
Namespace Netpix
{
Public Class Imagegenerator: ihttphandler
{
Public Bool Isreusable
{Get {Return True;}}
Public Void Processrequest (httpcontext context)
{
// Get the image filename and album root path from the database
// Image Browsing times
Int Numviews;
// ID in the Image Database
Int Picid = Convert. toint32 (context. request [ " ID " ]);
// Image path
String Imgpath = Npdata. getpathtopicture (picid, Out Numviews );
// Writing an image to output stream
Context. response. contenttype = " Image/jpg " ;
// 'Thumbnail 'means we are requesting a thumbnail
// Show thumbnails
If (Context. request [ " Thumbnail " ] ! = Null )
{
// Need to load the image, resize it, and stream to the client.
// Calculate the scale so as not to stretch or distort the image.
Bitmap BMP = New Bitmap (imgpath );
Float Scale = 150366f / System. Math. Max (BMP. Height, BMP. width );
System. Drawing. Image thumb = BMP. getthumbnailimage (( Int ) (BMP. Width * Scale ),( Int ) (BMP. Height * Scale ), Null , System. intptr. Zero );
Thumb. Save (context. response. outputstream, system. Drawing. imaging. imageformat. JPEG );
BMP. Dispose ();
Thumb. Dispose ();
}
Else
{
// Stream directly from the file
// Get the stream and send it out the response
System. Io. filestream FS = File. Open (imgpath, filemode. Open, fileaccess. Read, fileshare. Read );
Const Int Bytelength = 8192 ;
Byte [] Bytes = New Byte [Bytelength];
While (FS. Read (bytes, 0 , Bytelength) ! = 0 )
{
Context. response. binarywrite (bytes );
}
FS. Close ();
// Update database browsing times
Npdata. setnumviews (picid, numviews + 1 );
}
}
}
}
Usage:
Imgctrl. imageurl = "genimage. ashx? Id = "+ request [" ID "];