在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
实现原理:
using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.ComponentModel; using System.Drawing.Design; using System.Drawing.Drawing2D; using System.Text; using System.IO; namespace BlogLan.Web.Controls { /// <summary> /// 缩略图控件。 /// </summary> [DefaultProperty("ImageUrl")] [ToolboxData("<{0}:Thumbnail runat=server></{0}:Thumbnail>")] [DesignerAttribute(typeof(Designer.ThumbnailDesigner))] [Description("缩略图控件。")] public class Thumbnail : WebControl { public Thumbnail() : base(HtmlTextWriterTag.Img) { this.Width = Unit.Parse("100"); this.Height = Unit.Parse("75"); } //Private Members private bool urlResolved; /// <summary> /// 获取或设置图片路径。 /// </summary> [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Description("获取或设置图片路径"), Bindable(true), Category("Appearance"), DefaultValue(""), UrlProperty] public string ImageUrl { get { if (this.ViewState["ImageUrl"] != null) { return (string)this.ViewState["ImageUrl"]; } return string.Empty; } set { this.ViewState["ImageUrl"] = value; } } /// <summary> /// 获取或设置Jpeg缩略图的质量,值范围-100。 /// </summary> [DefaultValue(80)] public int JpegQuality { get { if (this.ViewState["JpegQuility"] != null) { return (int)this.ViewState["JpegQuility"]; } return 80; } set { if (value > 100) { this.ViewState["JpegQuility"] = 100; } else if (value < 20) { this.ViewState["JpegQuility"] = 20; } else { this.ViewState["JpegQuility"] = value; } } } [DefaultValue(typeof(Unit), "100px")] public override Unit Width { get { return base.Width; } set { base.Width = value; } } [DefaultValue(typeof(Unit), "75px")] public override Unit Height { get { return base.Height; } set { base.Height = value; } } internal bool UrlResolved { get { return this.urlResolved; } set { this.urlResolved = value; } } /// <summary> /// 获取或设置控件相对于网页上其他元素的对齐方式。 /// </summary> [DefaultValue(ImageAlign.NotSet), Description("获取或设置控件相对于网页上其他元素的对齐方式。")] public virtual ImageAlign ImageAlign { get { object obj2 = this.ViewState["ImageAlign"]; if (obj2 != null) { return (ImageAlign)obj2; } return ImageAlign.NotSet; } set { if ((value < ImageAlign.NotSet) || (value > ImageAlign.TextTop)) { throw new ArgumentOutOfRangeException("value"); } this.ViewState["ImageAlign"] = value; } } /// <summary> /// [禁用Enabled属性在任何位置编辑。] /// </summary> [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public override bool Enabled { get { return base.Enabled; } set { base.Enabled = value; } } /// <summary> /// 如无法显示图片时显示的替代文本。 /// </summary> [DefaultValue(""), Bindable(true), Localizable(true), Description("如无法显示图片时显示的替代文本。")] public virtual string AlternateText { get { if (this.ViewState["AlternateText"] != null) { return (string)this.ViewState["AlternateText"]; } return string.Empty; } set { this.ViewState["AlternateText"] = value; } } /// <summary> /// 获取或设置一个值,该值指示控件是否生成空字符串值的替换文字属性。 /// </summary> [DefaultValue(true), Description("获取或设置一个值,该值指示控件是否生成空字符串值的替换文字属性。")] public virtual bool GenerateEmptyAlternateText { get { if (this.ViewState["GenerateEmptyAlternateText"] != null) { return (bool)this.ViewState["GenerateEmptyAlternateText"]; } return true; } set { this.ViewState["GenerateEmptyAlternateText"] = value; } } /// <summary> /// 获取或设置图像详细说明的位置。 /// </summary> [Editor("System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), UrlProperty, DefaultValue(""), Description("获取或设置图像详细说明的位置。")] public virtual string DescriptionUrl { get { string str = (string)this.ViewState["DescriptionUrl"]; if (str != null) { return str; } return string.Empty; } set { this.ViewState["DescriptionUrl"] = value; } } //Methods /// <summary> /// 重写AddAttributesToRender方法。 /// </summary> /// <param name="writer"></param> protected override void AddAttributesToRender(HtmlTextWriter writer) { if (string.IsNullOrEmpty(this.Page.Request["thumbnail"])) { this.AddThumbnailAttributesToRender(writer, string.Empty); } } protected virtual internal void AddThumbnailAttributesToRender(HtmlTextWriter writer, string ForcedImageUrl) { base.AddAttributesToRender(writer); string imageUrl = this.ImageUrl; if (!this.UrlResolved) { imageUrl = base.ResolveClientUrl(imageUrl); } //在设计时强制赋予图形路径。 if (!string.IsNullOrEmpty(ForcedImageUrl)) { imageUrl = ForcedImageUrl; writer.AddAttribute(HtmlTextWriterAttribute.Src, imageUrl); } else { writer.AddAttribute(HtmlTextWriterAttribute.Src, this.Page.Request.Url.AbsolutePath + "?" + this.GetQueryString(), false); } imageUrl = this.DescriptionUrl; if (imageUrl.Length != 0) { writer.AddAttribute(HtmlTextWriterAttribute.Longdesc, base.ResolveClientUrl(imageUrl)); } imageUrl = this.AlternateText; if ((imageUrl.Length > 0) || this.GenerateEmptyAlternateText) { writer.AddAttribute(HtmlTextWriterAttribute.Alt, imageUrl); } switch (this.ImageAlign) { case ImageAlign.Left: writer.AddAttribute(HtmlTextWriterAttribute.Align, "left"); break; case ImageAlign.Right: writer.AddAttribute(HtmlTextWriterAttribute.Align, "right"); break; case ImageAlign.Baseline: writer.AddAttribute(HtmlTextWriterAttribute.Align, "baseline"); break; case ImageAlign.Top: writer.AddAttribute(HtmlTextWriterAttribute.Align, "top"); break; case ImageAlign.Middle: writer.AddAttribute(HtmlTextWriterAttribute.Align, "middle"); break; case ImageAlign.Bottom: writer.AddAttribute(HtmlTextWriterAttribute.Align, "bottom"); break; case ImageAlign.AbsBottom: writer.AddAttribute(HtmlTextWriterAttribute.Align, "absbottom"); break; case ImageAlign.AbsMiddle: writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle"); break; case ImageAlign.NotSet: break; default: writer.AddAttribute(HtmlTextWriterAttribute.Align, "texttop"); break; } if (this.BorderWidth.IsEmpty) { writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0px"); } } private string GetQueryString() { StringBuilder sb = new StringBuilder(); sb.Append("thumbnail=" + this.Page.Server.HtmlEncode(this.ImageUrl)); sb.Append("&w=" + this.Width.Value.ToString()); sb.Append("&h=" + this.Height.Value.ToString()); sb.Append("&bc=" + this.BackColor.ToArgb().ToString()); sb.Append("&jq=" + this.JpegQuality.ToString()); return sb.ToString(); } protected override void RenderContents(HtmlTextWriter writer) { } protected override ControlCollection CreateControlCollection() { return new EmptyControlCollection(this); } protected override void OnInit(EventArgs e) { base.OnInit(e); this.Init += new EventHandler(Thumbnail_Init); this.PreRender += new EventHandler(Thumbnail_PreRender); } void Thumbnail_PreRender(object sender, EventArgs e) { if (HttpContext.Current != null) { RenderThumbnailImage(this.Page.Request, this.Page.Response); } } void Thumbnail_Init(object sender, EventArgs e) { if (HttpContext.Current != null) { RenderThumbnailImage(this.Page.Request, this.Page.Response); } } static void RenderThumbnailImage(HttpRequest request, HttpResponse response) { if (string.IsNullOrEmpty(request["thumbnail"])) { return; } Thumbnail thumbnail = new Thumbnail(); Bitmap tmb; try { thumbnail.ImageUrl = request["thumbnail"]; try { thumbnail.Width = Unit.Parse(request["w"]); } catch { thumbnail.Width = Unit.Pixel(100); } try { thumbnail.Height = Unit.Parse(request["h" |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论