Asp. NET image Display method Examples _ practical skills

Source: Internet
Author: User

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.

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.