Display of images in ASP. NET

Source: Internet
Author: User
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 "];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.