我利用 Graphics 绘制了一个矩形的验证码图片框,
用 private string CheckCode() 方法获得了个随机产生的 字母加数字 组合的字符串。调试的时候 图片上 字符串参数 也就是g.DrawString(checkCode, font,new SolidBrush(Color.Red), 2, 2); 这句的 checkCode 等于CheckCode() 方法获得的字符串,为什么显示出来的图片 的字符串 不是 字符串参数呢?
比如 g.DrawString(checkCode, font,new SolidBrush(Color.Red), 2, 2); 的checkCode="AB2C";
实际运行的话,绘制的图片显示出来的不是AB2C; 这是为什么呢?
是不是我的代码错了 ,请高手指点,小弟先谢谢了!using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Data.SqlClient;
namespace Ex12_16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
CodeImage(CheckCode());
} private string CheckCode()
{
int number;
char code;
string checkCode = String.Empty; 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 % 10)); checkCode +=" "+code.ToString();
}
return checkCode;
} private void CodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 9.5)), 22);
Graphics g = Graphics.FromImage(image); try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 3; 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.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
g.DrawString(checkCode, font,new SolidBrush(Color.Red), 2, 2); //画图片的前景噪音点
for (int i = 0; i < 150; 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);
this.pictureBox1.Width=image.Width;
this.pictureBox1.Height = image.Height;
this.pictureBox1.BackgroundImage = image;
}
catch
{ }
} private void button1_Click(object sender, EventArgs e)
{
CodeImage(CheckCode()); } // private void button2_Click(object sender, EventArgs e) |||||||||||||||||||||||||||||||||||||
//{
// if (textBox1.Text == CheckCode()) { 我在这试了下 根本不可能看相同,图片上的和代码随机生成的不一样。 // MessageBox.Show("Test"); |||||||||||||||||||||||||||||||||||||
// }
}
}
}
g.DrawString(checkCode, font,new SolidBrush(Color.Red), 2, 2); // 这句绘制了图片上的字符,在checkCode, 但是假如通过CheckCode()方法获得字符后,显示到 绘制的图片上的字符和实际方法得到是字符 不是一样的,这是为什么?很苦恼啊 ---------- !!
用 private string CheckCode() 方法获得了个随机产生的 字母加数字 组合的字符串。调试的时候 图片上 字符串参数 也就是g.DrawString(checkCode, font,new SolidBrush(Color.Red), 2, 2); 这句的 checkCode 等于CheckCode() 方法获得的字符串,为什么显示出来的图片 的字符串 不是 字符串参数呢?
比如 g.DrawString(checkCode, font,new SolidBrush(Color.Red), 2, 2); 的checkCode="AB2C";
实际运行的话,绘制的图片显示出来的不是AB2C; 这是为什么呢?
是不是我的代码错了 ,请高手指点,小弟先谢谢了!using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Data.SqlClient;
namespace Ex12_16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
CodeImage(CheckCode());
} private string CheckCode()
{
int number;
char code;
string checkCode = String.Empty; 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 % 10)); checkCode +=" "+code.ToString();
}
return checkCode;
} private void CodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 9.5)), 22);
Graphics g = Graphics.FromImage(image); try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 3; 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.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
g.DrawString(checkCode, font,new SolidBrush(Color.Red), 2, 2); //画图片的前景噪音点
for (int i = 0; i < 150; 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);
this.pictureBox1.Width=image.Width;
this.pictureBox1.Height = image.Height;
this.pictureBox1.BackgroundImage = image;
}
catch
{ }
} private void button1_Click(object sender, EventArgs e)
{
CodeImage(CheckCode()); } // private void button2_Click(object sender, EventArgs e) |||||||||||||||||||||||||||||||||||||
//{
// if (textBox1.Text == CheckCode()) { 我在这试了下 根本不可能看相同,图片上的和代码随机生成的不一样。 // MessageBox.Show("Test"); |||||||||||||||||||||||||||||||||||||
// }
}
}
}
g.DrawString(checkCode, font,new SolidBrush(Color.Red), 2, 2); // 这句绘制了图片上的字符,在checkCode, 但是假如通过CheckCode()方法获得字符后,显示到 绘制的图片上的字符和实际方法得到是字符 不是一样的,这是为什么?很苦恼啊 ---------- !!
解决方案 »
- c# udp可不可以跨网段发送广播?
- 採用哪種方式性能最好
- 问一个条码标签打印的问题!~
- 达人怎么在.cs文件中验证email的有效
- 一个LIST<INT>型,里面有N个数,求最大数为多少,并把它赋给一个LABLE.TEXT中
- C#如何对结构体中的数组进行操作?
- 急急急!如何在Datagrid中实现与用户交互功能?
- 请问谁有域名查询和域名注册的开发包或相关文档.
- 在C#中如何将数据库里的数据导出到Excel电子表格中去(数据库是Access数据库)
- 如何做一个象word那样的在程序一开始的时候出来一个好看的图片,等主程序打开了自然消失的程序???
- AjaxPro2 返回DataSet 时出现" 缺少对象"错误
- vista与 .net的兼容性问题?
而不是Check的时候再去生成