#region[ BinaryImage Show Coponent]
///Author:Region
///MSN:[email protected]
///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;
}
}
} |