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

📄 useravatarhttphandler.cs

📁 ASP.NET简洁论坛源代码 这是一个简单的论坛
💻 CS
字号:
using System;
using System.Web;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.IO;
using System.Web.UI;
using NetFocus.Web.Core;

namespace NetFocus.Web.Applications.Forum
{
    public class UserAvatarHttpHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                int userId = int.Parse(context.Request.QueryString["userId"]);
                int picWidth = int.Parse(context.Request.QueryString["pw"]);
                int picHeight = int.Parse(context.Request.QueryString["ph"]);
                string backColor = context.Request.QueryString["bc"];
                bool useRealSize = bool.Parse(context.Request.QueryString["urs"]);

                ForumUser user = ForumUsers.GetUser(userId);
                if (user == null)
                {
                    return;
                }
                Avatar userAvatar = user.GetAvatar();
                if (userAvatar.Content == null)
                {
                    return;
                }

                Image image = System.Drawing.Image.FromStream(new MemoryStream(userAvatar.Content));
                ImageFormat format = image.RawFormat;

                Size newSize = Globals.GetNewSize(picWidth, picHeight, image.Width, image.Height);

                Bitmap outBitmap = null;

                if (!useRealSize)
                {
                    outBitmap = new Bitmap(picWidth, picHeight);

                    Graphics g = Graphics.FromImage(outBitmap);
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    if (!string.IsNullOrEmpty(backColor))
                    {
                        g.Clear(ColorTranslator.FromHtml("#" + backColor));
                    }
                    else
                    {
                        g.Clear(Color.White);
                    }
                    g.DrawImage(image, new Rectangle((picWidth - newSize.Width) / 2, (picHeight - newSize.Height) / 2, newSize.Width, newSize.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

                    g.Dispose();

                }
                else
                {
                    outBitmap = new Bitmap(newSize.Width, newSize.Height);

                    Graphics g = Graphics.FromImage(outBitmap);
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.DrawImage(image, new Rectangle(0, 0, newSize.Width, newSize.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

                    g.Dispose();
                }

                if (format.Equals(ImageFormat.Gif))
                {
                    context.Response.ContentType = @"image/gif";
                }
                else
                {
                    context.Response.ContentType = @"image/jpeg";
                }

                EncoderParameters encoderParams = new EncoderParameters();
                long[] quality = new long[1];

                quality[0] = 100;

                EncoderParameter encoderParameter = new EncoderParameter(Encoder.Quality, quality);

                encoderParams.Param[0] = encoderParameter;

                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();

                ImageCodecInfo jpegICI = null;

                for (int i = 0; i < arrayICI.Length; i++)
                {
                    if (arrayICI[i].FormatDescription.Equals("JPEG"))
                    {
                        jpegICI = arrayICI[i];
                        break;
                    }
                }

                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(30));
                context.Response.Cache.SetAllowResponseInBrowserHistory(true);
                context.Response.Cache.SetValidUntilExpires(true);
                context.Response.Cache.VaryByParams["tag"] = true;

                if (jpegICI != null)
                {
                    outBitmap.Save(context.Response.OutputStream, jpegICI, encoderParams);
                }
                else
                {
                    outBitmap.Save(context.Response.OutputStream, format);
                }
            }
            catch
            { }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

⌨️ 快捷键说明

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