如何显示ASP.NET中图片-创新互联

这篇文章主要介绍“如何显示ASP.NET中图片”,在日常操作中,相信很多人在如何显示ASP.NET中图片问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何显示ASP.NET中图片”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联是一家集网站建设,江汉企业网站建设,江汉品牌网站建设,网站定制,江汉网站建设报价,网络营销,网络优化,江汉网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

具体如下:

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"];

到此,关于“如何显示ASP.NET中图片”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


本文题目:如何显示ASP.NET中图片-创新互联
本文地址:http://myzitong.com/article/dcjspi.html