⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 useravatar.cs

📁 这是一个简单的论坛程序源码
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Security;
using NetFocus.Web.Core;
using System.Drawing;
using System.IO;

namespace NetFocus.Web.Applications.Forum
{
    public class UserAvatar : PlaceHolder
    {
        #region Private Members

        private int defaultWidth = 80;
        private int defaultHeight = 100;
        private ForumUser user;
        private WebContext context = WebContext.Current;

        #endregion

        #region Public Properties

        public ForumUser User
        {
            get
            {
                if (user == null)
                {
                    if (UserId > 0)
                    {
                        user = ForumUsers.GetUser(UserId);
                    }
                    if (user == null && !string.IsNullOrEmpty(UserName))
                    {
                        user = ForumUsers.GetUser(UserName);
                    }
                    if (user == null && context.UserId > 0)
                    {
                        user = ForumUsers.GetUser(context.UserId);
                    }
                    if (user == null && !string.IsNullOrEmpty(context.UserName))
                    {
                        user = ForumUsers.GetUser(context.UserName);
                    }
                    if (user == null)
                    {
                        user = context.User;
                    }
                }
                return user;
            }
            set
            {
                user = value;
                if (user != null)
                {
                    ViewState["UserId"] = user.UserId;
                }
                else
                {
                    ViewState.Remove("UserId");
                }
            }
        }
        public int UserId
        {
            get
            {
                object state = ViewState["UserId"];
                if (state != null)
                {
                    return (int)state;
                }
                return 0;
            }
            set
            {
                ViewState["UserId"] = value;
            }
        }
        public string UserName
        {
            get
            {
                object state = ViewState["UserName"];
                if (state != null)
                {
                    return state as string;
                }
                return string.Empty;
            }
            set
            {
                ViewState["UserName"] = value;
            }
        }
        public string AvatarUrl
        {
            get
            {
                object state = ViewState["AvatarUrl"];
                if (state != null)
                {
                    return state as string;
                }
                return "userAvatar.aspx";
            }
            set
            {
                ViewState["AvatarUrl"] = value;
            }
        }
        public string AnonymousAvatarUrl
        {
            get
            {
                string state = (string)ViewState["AnonymousAvatarUrl"];
                if (state != null)
                {
                    return state;
                }
                return "~/utility/anonymous.gif";
            }
            set
            {
                ViewState["AnonymousAvatarUrl"] = value;
            }
        }
        public int Border
        {
            get
            {
                object state = ViewState["Border"];
                if (state != null)
                {
                    return (int)state;
                }
                return 0;
            }
            set
            {
                ViewState["Border"] = value;
            }
        }
        public int Height
        {
            get
            {
                object state = ViewState["Height"];
                if (state != null)
                {
                    return (int)state;
                }
                return 0;
            }
            set
            {
                ViewState["Height"] = value;
            }
        }
        public int Width
        {
            get
            {
                object state = ViewState["Width"];
                if (state != null)
                {
                    return (int)state;
                }
                return 0;
            }
            set
            {
                ViewState["Width"] = value;
            }
        }
        public bool IsCustomSize
        {
            get
            {
                return this.Height > 0 && this.Width > 0;
            }
        }
        public bool UseRealSize
        {
            get
            {
                object state = ViewState["UseRealSize"];
                if (state != null)
                {
                    return (bool)state;
                }
                return true;
            }
            set
            {
                ViewState["UseRealSize"] = value;
            }
        }
        public string BackColor
        {
            get
            {
                string state = (string)ViewState["BackColor"];
                if (state != null)
                {
                    return state;
                }
                return "FFFFFF";
            }
            set
            {
                ViewState["BackColor"] = value;
            }
        }

        #endregion

        #region Override Methods

        protected override void Render(HtmlTextWriter writer)
        {
            HtmlImage image = new HtmlImage();

            if (!User.IsAnonymous)
            {
                //如果指定了宽度和高度,则根据当前用户的头像比例,
                //以及指定的宽度和高度范围,计算出头像应该显示的大小。
                if (IsCustomSize)
                {
                    Avatar userAvatar = User.GetAvatar();
                    if (userAvatar != null && userAvatar.Length > 0 && userAvatar.Content != null)
                    {
                        SetImgSize(image, System.Drawing.Image.FromStream(new MemoryStream(userAvatar.Content)));
                    }
                }
                else
                {
                    image.Width = this.defaultWidth;
                    image.Height = this.defaultHeight;
                }
                image.Src = Globals.ApplicationPath + "/" + AvatarUrl + "?tag=" + Guid.NewGuid().ToString() + string.Format("&pw={0}&ph={1}&urs={2}&bc={3}&userId={4}", new string[] { image.Width.ToString(), image.Height.ToString(), UseRealSize.ToString(), BackColor, User.UserId.ToString() });
            }
            else
            {
                //则采用默认匿名头像显示
                string fileUrl = this.Page.Response.ApplyAppPathModifier(AnonymousAvatarUrl);
                string file = WebContext.Current.HttpContext.Server.MapPath(fileUrl);

                if (File.Exists(file))
                {
                    //如果指定了宽度和高度,则根据默认匿名头像的比例,
                    //以及指定的宽度和高度范围,计算出默认匿名头像应该显示的大小。
                    if (IsCustomSize)
                    {
                        SetImgSize(image, System.Drawing.Image.FromFile(file));
                        image.Src = Globals.ApplicationPath + string.Format(@"/image.aspx?pw={0}&ph={1}&fu={2}&tag={3}&urs={4}&bc={5}", new string[] { image.Width.ToString(), image.Height.ToString(), fileUrl, Guid.NewGuid().ToString(), UseRealSize.ToString(), BackColor });
                    }
                    else
                    {
                        image.Src = this.Page.Response.ApplyAppPathModifier(AnonymousAvatarUrl);
                        image.Width = this.defaultWidth;
                        image.Height = this.defaultHeight;
                    }
                }
                else if (IsCustomSize)
                {
                    image.Width = Width;
                    image.Height = Height;
                }
                else
                {
                    image.Width = this.defaultWidth;
                    image.Height = this.defaultHeight;
                }
            }

            image.Border = this.Border;
            image.RenderControl(writer);

        }

        #endregion

        #region Helpers

        private void SetImgSize(HtmlImage img, System.Drawing.Image avatar)
        {
            if (!UseRealSize)
            {
                img.Width = Width;
                img.Height = Height;
            }
            else
            {
                Size newSize = Globals.GetNewSize(Width, Height, avatar.Width, avatar.Height);
                img.Width = newSize.Width;
                img.Height = newSize.Height;
            }
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -