ASP.NETはIP署名画像を生成する

2821 ワード

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Web; 
using System.Web.SessionState; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.IO;
public partial class ErrorPage : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
    {
        string str = "heihei";
        bool cusWord = false;
        //             ,      30 
        if ((str == null) || (str.Trim().Length == 0))
        {
            str = "";
        }
        else
        {
            if (str.Length > 30)
            {
                str = str.Substring(0, 30) + "";
            }
            string badword = "haha";
            //       
            for (int i = 0; i < badword.Split('|').Length; i++)
            {
                str = str.Replace(badword.Split('|')[i], "[  ,    ]");
            }
            cusWord = true;
        }
        //          
        string IP = "Your IP:" + Request.UserHostAddress;
        string Language = "Language:" + Request.UserLanguages[0];
        string OS = "Your Os:" + Request.Browser.Platform;
        string Browser = "Browser:" + Request.Browser.Browser + " " + Request.Browser.Version;
        //     Image  
        System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Server.MapPath("ip.jpg"));
        Graphics g = Graphics.FromImage(copyImage);
        g.DrawImage(copyImage, 0, 0, copyImage.Width, copyImage.Height);
        Font f = new Font("courier new", 8, System.Drawing.FontStyle.Bold);
        Font f1 = new Font("  ", 9, System.Drawing.FontStyle.Underline);
        Brush b1 = new SolidBrush(Color.Blue);
        Brush b2 = new SolidBrush(Color.Red);
        Brush b3 = new SolidBrush(Color.Green);
        Brush b4 = new SolidBrush(Color.Black);
        if (cusWord)
        {
            g.DrawString(str, f1, b4, 80, 5);
            g.DrawString(IP, f, b1, 80, 18);
            g.DrawString(OS, f, b2, 80, 30);
            g.DrawString(Language, f, b2, 80, 42);
            g.DrawString(Browser, f, b3, 80, 54);
        }
        else
        {
            g.DrawString(IP, f, b1, 80, 10);
            g.DrawString(OS, f, b2, 80, 25);
            g.DrawString(Language, f, b2, 80, 40);
            g.DrawString(Browser, f, b3, 80, 55);
        }
        g.Dispose();
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        copyImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.ClearContent();
        Response.ContentType = "image/Jpeg";
        Response.BinaryWrite(ms.ToArray());
    }
}