我想自己建个jpg的图像,上面放些文字和背景,请问c#能做到吗?
谢谢
谢谢
解决方案 »
- httpHandlers在虚拟主机上不可用?
- jquery 给层动态设高度,但是层没变化
- 页面上太多内容要写入数据库了,大概80个字段,页面上80个收集数据的TextBox1~80。怎么样才能快速写入、读出数据库呢?
- 关于登录后用户名不显示的问题
- 关于采集的问题。
- ajax.net中的tabcontainer问题
- 将参数值sqlparameter转换成string失败
- dreamveaver 和 vs 2005 的混合使用
- Response.Cache.SetNoStore();这一句是什么意思
- 为何我用ASP.NET开发的Web应用程序,当网线拔掉后不能访问?
- 关于数据库字段中的datetime比较问题
- showModalDialog 如何设置width和Height
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();
}
}
using System.Drawing;
读取一个图片得到Image对象,调用Graphics.FromImage方法, 得到Graphics;或者是用Graphics的DrawImage把image画到Graphics上. 然后调用Graphics.DrawString方法在Graphics上写文字.
最后然后保存, 保存怎么做我还没想到
http://blog.csdn.net/knight94/archive/2006/03/24/637727.aspx
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();
}