我想自己建个jpg的图像,上面放些文字和背景,请问c#能做到吗?
谢谢

解决方案 »

  1.   

    生成验证码的,就是自己生成的,参考:
    protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Session["VNum"] = GenerateCheckCode();
            this.CreateCheckCodeImage(GenerateCheckCode());
        }
        private string GenerateCheckCode()
        {
            int number;
            char code;
            string checkCode = String.Empty;        System.Random random = new Random();        for (int i = 0; i < 4; i++)
            {
                number = random.Next();            if(number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));            checkCode += code.ToString();
            }        Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));        return checkCode;
        }    private void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 16.0)), 25);  //15.0是每个字符的宽度,25是高度
            Graphics g = Graphics.FromImage(image);        try
            {
                //生成随机生成器
                Random random = new Random();            //清空图片背景色
                g.Clear(Color.White);            //画图片的背景噪音线
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }            //15是字号大小
                Font font = new System.Drawing.Font("Arial", 15, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(checkCode, font, brush, 2, 2);            //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);                image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }            //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);            System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType = "image/Gif";
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
      

  2.   

    还要加上这个命名空间:
    using System.Drawing;
      

  3.   

    应该是可以在画布上做到这些的, 我没有做过, 大概提供一个思路参考一下吧
    读取一个图片得到Image对象,调用Graphics.FromImage方法, 得到Graphics;或者是用Graphics的DrawImage把image画到Graphics上. 然后调用Graphics.DrawString方法在Graphics上写文字.
    最后然后保存, 保存怎么做我还没想到
      

  4.   

    参看
    http://blog.csdn.net/knight94/archive/2006/03/24/637727.aspx
      

  5.   

    看看这个,生成验证码的
     Bitmap newBitmap = new Bitmap(86, 26, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(newBitmap);
            Random r = new Random();
            g.Clear(Color.White);
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, 86, 26), Color.Red, Color.Blue, 1.2f, true);
            
            for (int i = 0; i < 25; i++)
            {
                int x1 = r.Next(newBitmap.Width);
                int x2 = r.Next(newBitmap.Width);
                int y1 = r.Next(newBitmap.Height);
                int y2 = r.Next(newBitmap.Height);
                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }
            for (int i = 0; i < 100; i++)
            {
                int x = r.Next(newBitmap.Width);
                int y = r.Next(newBitmap.Height);
                newBitmap.SetPixel(x, y, Color.FromArgb(r.Next()));
            }
            string value = GenerateRandom(4);
            Font font = new Font("黑体", 14, System.Drawing.FontStyle.Bold);
            g.DrawString(value, font, brush,2, 2);
            g.DrawRectangle(new Pen(Color.Silver),0,0,85,25);
            //g.FillRectangle(new SolidBrush(Color.Green), 0, 12, 80, 1);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            newBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/gif";
            Response.BinaryWrite(ms.ToArray());
        }
        private static char[] constant =
            {
                '0','1','2','3','4','5','6','7','8','9',
                'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','
            };
        public static string GenerateRandom(int Length)
        {
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(47);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(47)]);
            }
            return newRandom.ToString();
        }
      

  6.   

    http://www.webjx.com/htmldata/2006-01-21/1137824356.html
      

  7.   

    WeekZero(星期零)的不错,可以参考