The example in this article describes the display method of the picture in asp.net. Share to everyone for your reference. Specifically as follows:
GENIMAGE.ASHX:
Copy Code code as follows:
<%@ WebHandler language= "C #" class= "Netpix. Imagegenerator "%>
Genimage.ashx.cs:
Copyright (C) 2003 by Greg Ennis//(Mailto:greg@ennis.net)///* Contents of this file are subject to the Artisti
C License (the "License").
You are not to use this file except in compliance with the License.
You could obtain a copy of the License at://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 {true; } public void ProcessRequest (HttpContext context) {//get "image filename and album Root path from
The database//Picture browsing times int numviews; ID int picid = convert.toint32 in the picture database (context.request["id"]); Picture 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//display thumbnail if (context.request["thumbnail"]!= null) {
Need to load the image, resize it, and stream to the client.
Calculate the "scale so" to stretch or distort the image.
Bitmap bmp = new Bitmap (Imgpath); Float scale = 150.0f/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 ' 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);
}
}
}
}
How to use:
Copy Code code as follows:
Imgctrl.imageurl = "genimage.ashx?id=" + request["id"];
I hope this article will help you with the ASP.net program design.