在Web页内显示Binary(位图)的组件(二)

 

一、            写在前面:

由于工作比较紧张!代码写多了头脑就会发热,所以程序员一定要注意休息,不要太多的熬夜,俗语说得好:“什么都可以有就是不可以有病!”;

昨晚体验了一下vs2005,真是不一样的感觉,不能不佩服M$的眼光和想象力。深远的影响到每一个程序员。无论是从界面体验、功能体贴、人性化、代码的简洁等哪方面考虑,他带给我们的是效率的提升,和把事情的简单化。简单就是最好的。

我们不需要再加班的梦想也许不会太远了

让我们一起来吧!

 

二、            原理:

好了!有时候我们总是喜欢说废话。但是有时候废话却很可以调节心情,那么就继续把:

asp.net显示位图的唯一方法(因为目前我只知道这么一个方法^_^)就是将ImagUrl的连接设到一个页面上,然后在那个叶面将位图Respone到叶面上就行了!不过就是麻烦点而已。就是这样:

src="region.binaryimage/fccc37ab-f4cb-4199-8332-7ff0168daad4.aspx"。

那么要把这样一种过程集成到组建里面去,就可以不用再为建一个目标叶面而浪费时间了,以方便又省事(因为我比较懒,所以我比较喜欢写一些组件以便满足我的惰性)。

经过周密的研究后(^_^)得出一个解决方案:

组件自动产生一个相关目录,在该目录中再建个配置文件,以便把响应重定位到我们自定义类型处理。

 

三、            组件技术:

那么为了实现这个伟大的组件(^_^),我们需要一些组件方面的技术。

1、也是最重要的一点就是叶面重定位技术,在配置文件节点

<?xml version="1.0" encoding="GB2312" ?>

<configuration>

    <system.web>

        <httpHandlers>

              <add verb="*" path="*" type="Region.Controls.BinaryImageHandler, region.binaryimage"/>

       </httpHandlers>

    </system.web>

</configuration>

重定位到我们自定义类型。

2、如何实现自定义类型。在.net framework 中提供一个接口来达到这个效果,我们的自定义类型Region.Controls.BinaryImageHandler必须继承自System.Web.IhttpHandler实现他的两个接口。(由于篇幅的关系,更深入的原理就不在这里细说了,感兴趣的话可以查看MSDN)

3、还有一点就是位图的如何从源页传到目标页的呢?由于我们没有目标页,所有处理都在我们的自定义类型当中处理,而HttpHandler已发的时候Session等对象都还没有实力化,唯一可行的方法就是位图保存在Application里面。用完之后删掉就可以了。

4、那么还有几个地方要注意,就是将位图保存在Application中的ID,因为是全局的。所以可以取个GUID来稿定。

基本上就可以这么高定了!

源码发到下面,新建个类库项目将下面代码Copy然后编译就可以了!:(因为不前我还没有空间提供下载,如果哪位好心朋友提供我可上传上去供大家方便下载)

#region[     BinaryImage Show Coponent]

///Author:Region

///MSN:hl_net@msn.com

///Blog:http://Blog.csdn.net/xinyulou

#endregion

 

using System;

using System.IO;

using System.Web;

using System.Resources;

using System.Reflection;

 

 

namespace Region.Controls

{

    /// <summary>

    /// 显示位图 

    /// </summary>

    /// <code>

    /// <%@ Register TagPrefix="Region" Namespace="Region.Controls" Assembly="Region.Controls" %>

    /// <Region:BinaryImage id="BinaryImage" runat="server"></Region:BinaryImage>

    /// </code>

    //[System.Web.UI.ToolboxData("<{0}:BinaryImage runat=server></{0}:BinaryImage>")]

    public class BinaryImage:System.Web.UI.WebControls.Image

    {  

       string path1 ;

       protected override void OnLoad(EventArgs e)

       {

           base.OnLoad (e);

           path1 = Path.Combine(Page.Request.PhysicalApplicationPath,Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location));      

           string path2 = Path.Combine(path1,"Web.config");       

           if (!Directory.Exists(path1))

           {

              Directory.CreateDirectory(path1);

           }

           if(!File.Exists(path2))

           {

//            ResourceManager rm = new ResourceManager(Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location)+".String", Assembly.GetExecutingAssembly());

//            string a = rm.GetString("Config");    

              string a = "<?xml version=/"1.0/" encoding=/"utf-8/" ?>"+@"

<configuration>"+@"

    <system.web>"+@"

        <httpHandlers>"+@"

              "+"<add verb=/"*/" path=/"*/" type=/""+this.GetType().Namespace+".BinaryImageHandler, "+Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location)+"/"/>"+@"

       </httpHandlers>"+@"

    </system.web>"+@"

</configuration>"+@"

";

              byte[] bs = System.Text.Encoding.Default.GetBytes(a);

              FileStream fileStream = new FileStream(path2,FileMode.Create, FileAccess.Write);

              try

              {

                  fileStream.Write(bs, 0,bs.Length);

              }

              finally

              {

                  if (fileStream != null)

                  {

                     ((IDisposable)fileStream).Dispose();

                  }

              }

           }

          

       }

 

       //唯一ID

       public string GUID

       {

           get

           {

              if( ViewState[UniqueID]== null)

                  ViewState[UniqueID] = Guid.NewGuid().ToString();

              return (string)ViewState[UniqueID];

           }

       }

 

       //[System.ComponentModel.Browsable(false)]

       public byte[]  Binary

       {  

           set{ Page.Session[GUID] = value;}

           get{return Page.Session[GUID]==null?null:(byte[])Page.Session[GUID];}

       }

 

       protected override void Render(System.Web.UI.HtmlTextWriter writer)

       {

           if(ImageUrl == "")

           {

              ImageUrl = Path.Combine(Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location), GUID+".aspx");

              if(Page.Application[GUID] == null)

                  Page.Application[GUID] = Binary;  

           }

           base.Render(writer);

       }

   

    }

 

    public class BinaryImageHandler:System.Web.IHttpHandler

    {

       public virtual bool IsReusable

       {

           get{return true;}

       }

       public virtual void ProcessRequest(HttpContext context)

       {

           string id = Path.GetFileNameWithoutExtension(context.Request.FilePath);

           if(context.Application[id] != null)

           {

              byte[] bs = (byte[])context.Application[id];           

              HttpContext.Current.Response.BinaryWrite(bs);

              context.Application[id] = null;

           }

       }

    }

 

 

四、            写在最后:

 

边听着悠扬美妙的音乐、边体验Codes的诗一般的美。

如此而已

 

Share u opinion

如果你有什么想法,欢迎写下来!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值