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
图片显示
,以便于您获取更多的相关知识。

时间: 2024-08-04 04:15:56

ASP.NET中图片显示方法实例_实用技巧的相关文章

ASP.NET中用js取CheckBoxList中值的方法实例_实用技巧

做的一些项目都比较小,而且时间紧,有好多东西都没来得急总结,趁这会还有点时间把前面项目中的用到的知识点分享下,只为以后方便使用.前台页面代码 复制代码 代码如下: <!--关键字-->    <div id="keyWordsDiv" style="border: 2px solid #6FA1D9; display: none; position: absolute;        top: 0px; left: 0px; width: 260px; he

ASP.NET加密口令的方法实例_实用技巧

每当我们要建立数据库驱动的个人化的web站点时,都必须要保护用户的数据.尽管黑客可以盗取个人的口令,然而更严重的问题是有人能够盗走整个数据库,然后立刻就是所有的口令. 原理 有一个好的做法是不将实际的口令存储在数据库中,而是存储它们加密后的版本.当我们需要对用户进行鉴定时,只是对用户的口令再进行加密,然后将它与系统中的加密口令进行比较即可. 在ASP中,我们不得不借助外部对象来加密字符串.而.NET SDK解决了这个问题,它在System.Web.Security名称空间中的FormsAuthe

将Access数据库中数据导入到SQL Server中的详细方法实例_实用技巧

Default.aspx 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AccessToSQL.aspx.cs" Inherits="AccessToSQL" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

在ASP.NET中插入flash代码实例_实用技巧

在需要插入Flash的地方插入以下代码: 复制代码 代码如下: <OBJECT codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"         classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">        <PARAM NAME="_cx

ASP.NET中XML转JSON的方法实例_实用技巧

本文实例讲述了ASP.NET中XML转JSON的方法,分享给大家供大家参考.具体如下: 一般在许多应用程序中都将数据存储为XML的格式,而且会将数据以JSON的格式发送到客户端以做进一步处理.要实现这一点,它们必须将XML格式转换为JSON格式. XML转JSON代码如下: 复制代码 代码如下: private static string XmlToJSON(XmlDocument xmlDoc)  {      StringBuilder sbJSON = new StringBuilder(

ASPX中的用户控件与ASP中的INCLUDE方法对比_实用技巧

    在ASP的年代里,为了避免经常性重复的劳动,对一些功能相似的区域或者代码,经常作成一个文件,然后通过连接(直接连接或者虚拟连接)的方法引入到ASP网页文件之中,对于一个很大的引用了很多ASP文件就相当于一个文件被分成了很多块,彼此文件之间的数据是可以自由共享的(除了函数之中的数据).    ASPX的用户控件就与INCLUDE有很大的不同了,它的最大特点就是在于他是以包装好的对象的形式呈现,通过我们的编程,可以将一个公用的事例抽象出来,将一些功能和方法总结出来,作成相应的函数和属性供外部

ASP.NET实现伪静态网页方法小结_实用技巧

本文实例总结了ASP.NET实现伪静态网页方法,分享给大家供大家参考之用.具体方法如下: 方法一:利用Httphandler实现URL重写(伪URL及伪静态) 我们有时候会见到这样的地址:"http://www.XXXX.com/show-12-34.html",你或许认为在站点服务器根目录"/"下存在名为"show-12-34.html"的文件,其实实际它可能是不存在的,而可能你看到的内容是"/aspx/show.aspx?type=

asp.net中SqlCacheDependency缓存技术概述_实用技巧

本文实例讲述了asp.net中SqlCacheDependency缓存技术,对于大型web程序设计来说具有很高的实用价值.具体如下: 对于访问量大,但更新较少的网站中使用缓存技术,可以大大提高运行效率:加上.NET 2.0提供的缓存依赖机制,我们可以很方便的对缓存进行管理更新:以下是本人学习的一点心得体会,希望能够起到抛砖引玉的作用. 建立缓存依赖,实现代码如下: /**//// <summary> /// 建立缓存依赖项 /// </summary> /// <return

LiteralControl ASP.NET中的另类控件_实用技巧

首先看一个aspx文件里的部分内容: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="