?? binaryimage.cs
字號:
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.IO;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
[assembly: WebResource("myControls.WeblRes.BinaryImage_Desgin.png", "image/x-png")]
[assembly: WebResource("myControls.WeblRes.BinaryImage_Erro.png", "image/x-png")]
namespace myControls
{
[ToolboxData("<{0}:BinaryImage runat=server></{0}:BinaryImage>"), Designer(typeof(BinaryImageDesigner)), Description("")]
public class BinaryImage : WebControl
{
private byte[] m_Binary;
private bool m_FixIsDefault;
public BinaryImage()
{
}
#region 相關屬性
#region 公開屬性
[Browsable(false), Description("圖片二進制數據")]
public byte[] Binary
{
get { return m_Binary; }
set { m_Binary = value; }
}
[DefaultValue(false), Description("是否以固定圖片為首選項:true優先顯示固定圖片,false則優先顯示二進制圖片")]
public bool FixIsDefault
{
get { return m_FixIsDefault; }
set { m_FixIsDefault = value; }
}
[DefaultValue(""), Description("設置固定圖片地址"), Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
public virtual string FixImageSrc
{
get
{
object obj = this.ViewState["FixImageSrc"];
return (obj == null) ? string.Empty : obj.ToString();
}
set
{
this.ViewState["FixImageSrc"] = value;
}
}
[DefaultValue(0), Category("Layout"), Description("圖片對齊方式")]
public virtual System.Web.UI.WebControls.ImageAlign ImageAlign
{
get
{
object obj1 = this.ViewState["ImageAlign"];
if (obj1 != null)
{
return (System.Web.UI.WebControls.ImageAlign)obj1;
}
return System.Web.UI.WebControls.ImageAlign.NotSet;
}
set
{
if ((value < System.Web.UI.WebControls.ImageAlign.NotSet) || (value > System.Web.UI.WebControls.ImageAlign.TextTop))
{
throw new ArgumentOutOfRangeException("value");
}
this.ViewState["ImageAlign"] = value;
}
}
#endregion
private bool IsImageShowing
{
get
{
bool flag1;
try
{
flag1 = (this.Page.Request["BinaryImage"] != null) && (this.Page.Request["BinaryImage"] == this.ClientID);
}
catch
{
flag1 = false;
}
return flag1;
}
}
private bool IsNothing
{
get
{
if ((this.Binary != null) && (this.Binary.Length != 0) || !string.IsNullOrEmpty(this.FixImageSrc))
{
return false;
}
return true;
}
}
protected virtual string ImageUrl
{
get
{
NameValueCollection col = new NameValueCollection();
col.Add("BinaryImage", this.ClientID);
return BuildUrlString(col);
}
}
#endregion
protected override void OnLoad(EventArgs e)
{
if (this.IsImageShowing)
{
if (!this.IsNothing)
this.RenderImage();
}
base.OnLoad(e);
}
protected override void Render(HtmlTextWriter writer)
{
writer.WriteBeginTag("img");
if (this.TabIndex != 0)
{
writer.WriteAttribute("tabindex", this.TabIndex.ToString());
}
writer.WriteAttribute("border", "0");
if (!this.IsNothing)
{
writer.WriteAttribute("src", this.ImageUrl);
writer.WriteAttribute("Title", this.ToolTip);
}
else
{
writer.WriteAttribute("src", this.Page.ClientScript.GetWebResourceUrl(typeof(BinaryImage), "myControls.WeblRes.BinaryImage_Erro.png"));
writer.WriteAttribute("Title", "控件未曾初始化");
}
writer.WriteAttribute("id", this.ClientID + "_image");
if (!string.IsNullOrEmpty(this.CssClass))
writer.WriteAttribute("Class", this.CssClass);
if (!this.Width.IsEmpty)
writer.WriteAttribute("Width", this.Width.ToString());
if (!this.Height.IsEmpty)
writer.WriteAttribute("Height", this.Height.ToString());
#region 對齊方式
System.Web.UI.WebControls.ImageAlign align1 = this.ImageAlign;
if (align1 != System.Web.UI.WebControls.ImageAlign.NotSet)
{
string text2;
switch (align1)
{
case System.Web.UI.WebControls.ImageAlign.Left:
{
text2 = "left";
break;
}
case System.Web.UI.WebControls.ImageAlign.Right:
{
text2 = "right";
break;
}
case System.Web.UI.WebControls.ImageAlign.Baseline:
{
text2 = "baseline";
break;
}
case System.Web.UI.WebControls.ImageAlign.Top:
{
text2 = "top";
break;
}
case System.Web.UI.WebControls.ImageAlign.Middle:
{
text2 = "middle";
break;
}
case System.Web.UI.WebControls.ImageAlign.Bottom:
{
text2 = "bottom";
break;
}
case System.Web.UI.WebControls.ImageAlign.AbsBottom:
{
text2 = "absbottom";
break;
}
case System.Web.UI.WebControls.ImageAlign.AbsMiddle:
{
text2 = "absmiddle";
break;
}
default:
{
text2 = "texttop";
break;
}
}
writer.AddAttribute(HtmlTextWriterAttribute.Align, text2);
}
#endregion
writer.Write('>');
}
private void RenderImage()
{
byte[] byte1;
if (this.FixIsDefault)
{
byte1 = (string.IsNullOrEmpty(this.FixImageSrc)) ? this.Binary : GetFromFile(this.Page.Server.MapPath(this.FixImageSrc));
}
else
{
byte1 = ((this.Binary != null) && (this.Binary.Length != 0)) ? this.Binary : GetFromFile(this.Page.Server.MapPath(this.FixImageSrc));
}
RenderImage(byte1);
}
private string BuildUrlString(NameValueCollection col)
{
string currentUrl = Page.Request.Path;
NameValueCollection urlParams = Page.Request.QueryString;
int i;
string tempstr = "";
if (urlParams == null || urlParams.Count <= 0)
{
for (i = 0; i < col.Count; i++)
{
tempstr += String.Concat("&", col.Keys[i], "=", col[i]);
}
return String.Concat(currentUrl, "?", tempstr.Substring(1));
}
NameValueCollection newCol = new NameValueCollection(urlParams);
string[] newColKeys = newCol.AllKeys;
for (i = 0; i < newColKeys.Length; i++)
{
newColKeys[i] = newColKeys[i].ToLower();
}
for (i = 0; i < col.Count; i++)
{
if (Array.IndexOf(newColKeys, col.Keys[i].ToLower()) < 0)
newCol.Add(col.Keys[i], col[i]);
else
newCol[col.Keys[i]] = col[i];
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (i = 0; i < newCol.Count; i++)
{
sb.Append("&");
sb.Append(newCol.Keys[i]);
sb.Append("=");
sb.Append(newCol[i]);
}
return String.Concat(currentUrl, "?", sb.ToString().Substring(1));
}
#region 靜態方法
public static byte[] GetFromFile(string path)
{
FileInfo info1 = new FileInfo(path);
FileStream stream1 = info1.OpenRead();
byte[] buffer1 = new byte[stream1.Length];
stream1.Read(buffer1, 0, int.Parse(stream1.Length.ToString()));
return buffer1;
}
private static void RenderImage(byte[] byteImg)
{
System.Drawing.Image image1 = System.Drawing.Image.FromStream(new MemoryStream(byteImg));
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.BinaryWrite(byteImg);
HttpContext.Current.Response.End();
}
#endregion
}
[SupportsPreviewControl(true)]
public class BinaryImageDesigner : ControlDesigner
{
public override string GetDesignTimeHtml()
{
BinaryImage bImg = (BinaryImage)Component;
string imgUrl = bImg.Page.ClientScript.GetWebResourceUrl(typeof(BinaryImage), "myControls.WeblRes.BinaryImage_Desgin.png");
string imgWidth = bImg.Width.IsEmpty ? string.Empty : string.Format(" Width='{0}' ",bImg.Width.ToString());
string imgHeight = bImg.Height.IsEmpty ? string.Empty : string.Format(" Height='{0}' ",bImg.Height.ToString());
return string.Format("<img border='0' src='{0}'{1}{2} Title='設計模式'>", imgUrl, imgWidth, imgHeight);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -