ashx输出图片

<%@ WebHandler Language="C#" Class="scene_ticket_pic"%>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using WebFunction;
using BusinessFacade;
using CLibrary;
using System.IO;

public class scene_ticket_pic : IHttpHandler {
    HttpContext context;
    public bool IsReusable
    {
      get{return true;}
    }
    public void ProcessRequest (HttpContext context)
    {
        this.context=context;
        SceneCouponGdi.LoadDownImg(MutiSceneCoupon()); 
    }   
    private Bitmap MutiSceneCoupon()
    {
        ArrayList bitmaps = new ArrayList();
        Hashtable ht = SetInfo();
        int adultNum = Convert.ToInt32(ht["adult"]);
        if (adultNum > 0)
        {
            for (int i = 0; i < adultNum; i++)
            {
                bitmaps.Add(OneSceneCoupon(ht, i));
            }
            SceneCouponGdi sceneCouponGdi = new SceneCouponGdi();
            Bitmap MutiBitMap = sceneCouponGdi.MutiPhoto(bitmaps, 610, 229);
            return MutiBitMap;
        }
        else
        {
            context.Response.End();
            return null;
        }
    }
    private Bitmap OneSceneCoupon(Hashtable ht, int serialNumber)
    {
        SceneCouponGdi sceneCouponGdi = new SceneCouponGdi();
        sceneCouponGdi.CreatePhoto(610, 229);

        string backgroudImgPath = context.Server.MapPath(@"../img/ticket_bg.gif");
        sceneCouponGdi.LoadScenePhoto(backgroudImgPath, 302, 4, 300, 216);

        string sceneImgPath = ht["Logo"].ToString();
        sceneCouponGdi.LoadScenePhoto(sceneImgPath, 18, 65, 140, 140);

        //        string sText1 = @"旅游名店城 www.yocity.cn
        //            优惠价"+ht["price"].ToString()+@"元整";
        //        sceneCouponGdi.CreateWatermark(sText1, Color.Red, 10, "宋体", Color.Gray, 180);

        string sText = ht["scene"].ToString();
        sceneCouponGdi.CreatePlainText(38, 14, sText, "#000066", 14, "宋体", FontStyle.Bold);

        sText = ht["sceneticket"].ToString();
        sceneCouponGdi.CreatePlainText(250, 14, sText, "#000066", 14, "宋体", FontStyle.Bold);

        sText = @"优惠价:¥" + ht["price"].ToString();
        sceneCouponGdi.CreatePlainText(479, 14, sText, "#000066", 14, "宋体", FontStyle.Bold);

        sText = @"景区优惠券 YOCITY.CN    " + ht["webmember"].ToString() + @"    有效期:" + ht["usetime"].ToString() + @"    编号:" + ht["reserveid"].ToString() + "_" + serialNumber;
        sceneCouponGdi.CreatePlainText(24, 38, sText, "#000000", 12, "宋体", FontStyle.Regular);

        sText = ht["beizhu"].ToString();
        sceneCouponGdi.CreatePlainText(174, 69, sText, "#990000", 12, "宋体", FontStyle.Regular, 395, 100);

        sceneCouponGdi.CreateLine(Color.Blue, 178, 132, 555, 132);

        sText = @"* 凭券在景区售票处享受优惠购票。(一张优惠券仅限换购一张优惠门票)
* 涉及订房的优惠请预先与景区确认。
* 景区景点及本网站保留对本优惠券的最终使用解释权。
* 黄金周期间优惠券无效(除特别注明外)。";
        sceneCouponGdi.CreatePlainText(174, 143, sText, "#333333", 12, "宋体", FontStyle.Regular);

        return sceneCouponGdi.BmPhoto;
    }
    private Hashtable SetInfo()
    {
        Hashtable ht = new Hashtable();
        string webmemberid = context.User.Identity.Name;
        string reserveid = context.Request["reserveid"];
        ht["reserveid"] = reserveid;
        if (reserveid == null || reserveid == "")
        {
            context.Response.End();
        }
        COrder _COrder = new COrder();
        _COrder.reserveID = reserveid;

        if (_COrder.Init())
        {
            ht["webmember"] = _COrder.reserve_name;
        }
        else
        {
            context.Response.End();
        }
        if (_COrder.web_memberid != webmemberid)
        {
            context.Response.End();
            //context.Response.Redirect("../../error.aspx");
        }
        COrderScene _COrderScene = new COrderScene();
        _COrderScene.reserveID = reserveid;
        if (_COrderScene.Init())
        {

            string priceID = _COrderScene.priceid;
            if (priceID == null || priceID == "")
            {
                context.Response.End();
            }
            CSceneTicket _CSceneTicket = new CSceneTicket();
            CScene _CScene = new CScene();
            _CSceneTicket.priceID = priceID;
            if (_CSceneTicket.Init())
            {
                _CScene.sceneid = _CSceneTicket.sceneid;
                _CScene.Init();
                ht["scene"] = _CScene.scene;
                ht["sceneticket"] = _CSceneTicket.tkname;
                ht["price"] = _COrderScene.sale_adult;
                ht["beizhu"] = _COrderScene.beizhu;
                ht["usetime"] = CFormat.datetimeGet(_COrderScene.godate).ToShortDateString() + "至" + CFormat.datetimeGet(_COrderScene.godate).AddDays(7).ToShortDateString();
                ht["other_name"] = _COrderScene.other_name;
                ht["adult"] = _COrderScene.adult;
                string picLogo = _CScene.logo;
                if (picLogo == string.Empty)
                {
                    ht["Logo"] = context.Server.MapPath(@"../img/ticket_logo.gif");
                }
                else
                {
                    string appSettingValue = System.Configuration.ConfigurationManager.AppSettings["AdminPhysicalPath"].ToString();
                    string dbPicPath = (appSettingValue + picLogo).Replace('/', '//');
                    if (File.Exists(dbPicPath))
                    {
                        ht["Logo"] = dbPicPath;
                    }
                    else
                    {
                        ht["Logo"] = context.Server.MapPath(@"../img/ticket_logo.gif");
                    }

                }

            }
        }
        return ht;
    }
}

==============================================================

以下转:http://www.cnblogs.com/zgqys1980/archive/2008/03/11/1100863.html

一提到Ashx文件,我们就会想到http handler以及图片加载(在之前我们一般使用ASPX或者Webservice去做),一般做法如下:


Handler.ashx:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.IO;
using System.Web;
public class Handler : IHttpHandler {


public bool IsReusable {
  get {
   return true;
  }
}
public void ProcessRequest (HttpContext context) {
  context.Response.ContentType = "image/jpeg";
  context.Response.Cache.SetCacheability(HttpCacheability.Public);
  context.Response.BufferOutput = false;
  PhotoSize size;
  switch (context.Request.QueryString["Size"]) {
   case "S":
    size = PhotoSize.Small;
    break;
   case "M":
    size = PhotoSize.Medium;
    break;
   case "L":
    size = PhotoSize.Large;
    break;
   default:
    size = PhotoSize.Original;
    break;
  }
  Int32 id = -1;
  Stream stream = null;
  if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "") {
   id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
   stream = PhotoManager.GetPhoto(id, size);
  } else {
   id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);
   stream = PhotoManager.GetFirstPhoto(id, size);
  }
  if (stream == null) stream = PhotoManager.GetPhoto(size);
  const int buffersize = 1024 * 16;
  byte[] buffer = new byte[buffersize];
  int count = stream.Read(buffer, 0, buffersize);
  while (count > 0) {
   context.Response.OutputStream.Write(buffer, 0, count);
   count = stream.Read(buffer, 0, buffersize);
  }
}
}


*.aspx:
<img src="myHttpHander.ashx?id=123" width="20" height="20" />


我们变通以下,发现其实除了可以输出图片以外,还可以输出文字:
Handler.ashx:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write("alert('hi')");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}


*.aspx:
弹出alert
<script src="Handler.ashx"></script>
也可以把.ashx当成css文件
<link href="css/Handler.ashx" rel="stylesheet" type="text/css">
xml文件
orderDoc.load("Handler.ashx");


还可以嵌入文字:
Handler.ashx:
<%@ WebHandler Language="C#" Class="TestHandler" %>
using System;
using System.Web;
public class TestHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write("document.write(/"Hello World/");");
    }


    public bool IsReusable {
        get {
            return false;
        }
    }
}
*.aspx:
<script type="text/javascript" src="TestHandler.ashx" />



当你希望从ashx或HttpHandler里访问你的Session时,你必须实现IReadOnlySessionState接口.

代码:

using System;
using System.Web;
using System.Web.SessionState;

public class DownloadHandler : IHttpHandler, IReadOnlySessionState
{
   public bool IsReusable { get { return true; } }
  
   public void ProcessRequest(HttpContext ctx)
   {
       ctx.Response.Write(ctx.Session["fred"]);
   }
}




其实,学习的思路不应该这样,以上除了图片外,我们都用偏了,为什么用偏了呢,因为软件以简单、实用为主,我们只是把以上纯粹看成可一项技术而没有把它放到软件的地位去考虑:)
具体的用途,大家可以参考Rewirte.dll (这个dll,可以使服务器支持伪静态的)!
反编译,看看人家做的:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值