ASP.NET中圖片的顯示

來源:互聯網
上載者: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 except in compliance with the License. 
// You may 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 { return true; } } 

        public void ProcessRequest(HttpContext Context) 
        { 
            // Get the image filename and album root path from the database
            //圖片瀏覽次數
            int numviews;
            //圖片資料庫中的ID
            int picid = Convert.ToInt32(Context.Request["id"]);
            //圖片路徑 
            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
            //顯示縮圖
            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 = 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 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();

                //更新資料庫瀏覽次數
                npdata.SetNumViews(picid, numviews+1);
            }
        }
    }
}


使用方法:
imgCtrl.ImageUrl = "genimage.ashx?id=" + Request["id"];

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.