BinaryImage.cs
资源名称:H3_OA.rar [点击查看]
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:7k
源码类别:
OA系统
开发平台:
C#
- using System;
- using System.Collections.Specialized;
- 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;
- namespace OThinker.H3.WorkSheet
- {
- [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; }
- }
- private string _FixImageSrc;
- [DefaultValue(""), Description("设置固定图片地址"), Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
- public virtual string FixImageSrc
- {
- get
- {
- return this._FixImageSrc;
- }
- set
- {
- this._FixImageSrc = value;
- }
- }
- private System.Web.UI.WebControls.ImageAlign _ImageAlign = ImageAlign.NotSet;
- [DefaultValue(0), Category("Layout"), Description("图片对齐方式")]
- public virtual System.Web.UI.WebControls.ImageAlign ImageAlign
- {
- get
- {
- return this._ImageAlign;
- }
- set
- {
- this._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) || !(this.FixImageSrc == null))
- {
- 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("Title", "控件未曾初始化");
- }
- writer.WriteAttribute("id", this.ClientID + "_image");
- if (!(this.CssClass == null))
- 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 = (this.FixImageSrc == null) ? 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
- }
- public class BinaryImageDesigner : ControlDesigner
- {
- public override string GetDesignTimeHtml()
- {
- BinaryImage bImg = (BinaryImage)Component;
- 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='设计模式'>", null, imgWidth, imgHeight);
- }
- }
- }