首页 > 编程 > .NET > 正文

随机图片生成器制作方法分享

2024-07-10 12:44:03
字体:
来源:转载
供稿:网友

代码如下:
/// <summary>
    /// PicHandler1 的摘要说明
    /// </summary>
    public class PicHandler1 : IHttpHandler, IRequiresSessionState
    {
        private string mCheckNo = string.Empty;
        protected ImgBuilder _ImgBuilder = new ImgBuilder();
        protected VryImgGen _ImgBuilderNew = new VryImgGen();
        private string _text = string.Empty;
        private string _font = "宋体";
        private int _fontSize = 8;
        private int _padding = 2;
        public void ProcessRequest(HttpContext context)
        {
            mCheckNo = DisCheckNo();
            context.Session["CheckCode"] = mCheckNo;
            this._ImgBuilder.FontSize = this._fontSize;
            this._ImgBuilder.Padding = this._padding;

            if (!string.IsNullOrEmpty(this._font))
            {
                this._ImgBuilder.Fonts = new string[] { this._font };
            }
            this._ImgBuilderNew.ChaosWight = 40;
            this._ImgBuilderNew.FontSize = 25;

            this._ImgBuilderNew.Padding = 3;
            System.Drawing.Bitmap image =
             this._ImgBuilderNew.CreateImage(mCheckNo);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            context.Response.ClearContent();
            context.Response.ContentType = "image/Jpeg";
            context.Response.BinaryWrite(ms.ToArray());
            context.Session["CheckCode"] = mCheckNo.ToString(); //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表