public VerifyImageInfo GenerateImage(string code, int width, int height, Color bgcolor, int textcolor)
{
VerifyImageInfo verifyimage = new VerifyImageInfo();
verifyimage.ImageFormat = ImageFormat.Jpeg;
verifyimage.ContentType = "image/pjpeg"; Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bitmap);
Rectangle rect = new Rectangle(0, 0, width, height);
g.SmoothingMode = SmoothingMode.AntiAlias; g.Clear(bgcolor); //int emSize = Next(3) + 15;//(int)((width - 20) * 2 / text.Length);
//FontFamily family = fonts[Next(fonts.Length - 1)];
//Font font = new Font(family, emSize, FontStyle.Bold); //SizeF measured = new SizeF(0, 0);
//SizeF workingSize = new SizeF(width, height);
//while (emSize > 2 && (measured = g.MeasureString(code, font)).Width > workingSize.Width || measured.Height > workingSize.Height)
//{
// font.Dispose();
// font = new Font(family, emSize -= 2);
//}
int fixedNumber = textcolor == 2 ? 60 : 0; SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100)));
for (int x = 0; x < 3; x++)
{
Pen linePen = new Pen(Color.FromArgb(Next(150) + fixedNumber, Next(150) + fixedNumber, Next(150) + fixedNumber),1);
g.DrawLine(linePen, new PointF(0.0F + Next(20), 0.0F + Next(height)), new PointF(0.0F + Next(width), 0.0F + Next(height)));
}
Matrix m = new Matrix();
for (int x = 0; x < code.Length; x++)
{
m.Reset();
m.RotateAt(Next(30) - 15, new PointF(Convert.ToInt64(width * (0.10 * x)), Convert.ToInt64(height * 0.5)));
g.Transform = m;
drawBrush.Color = Color.FromArgb(Next(150) + fixedNumber + 20, Next(150) + fixedNumber + 20, Next(150) + fixedNumber + 20);
PointF drawPoint = new PointF(0.0F + Next(4) + x * 20, 3.0F + Next(3));
g.DrawString(Next(1) == 1 ? code[x].ToString() : code[x].ToString().ToUpper(), fonts[Next(fonts.Length - 1)], drawBrush, drawPoint);
g.ResetTransform();
} double distort = Next(5, 10) * (Next(10) == 1 ? 1 : -1); using (Bitmap copy = (Bitmap)bitmap.Clone())
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 90)));
int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 80)));
if (newX < 0 || newX >= width)
newX = 0;
if (newY < 0 || newY >= height)
newY = 0;
bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
}
}
}
//g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); drawBrush.Dispose();
g.Dispose(); verifyimage.Image = bitmap; return verifyimage;
}
解决方案 »
- 最近想研究ASP技术,不知道如何学起。。。看什么
- 求一个sql语句
- xml.net
- 看看这个网站
- 教你一个看系统信息的命令
- 如何制作水晶报表.请指点,给个例子
- 当用户上传图片到服务器时,怎么把用户上传的图片生成32*32的然后再存到服务器的硬盘上?
- 我要得到FBC_1中的1,请问各位高手怎么载取呀!!在线等,好了马上给分!!
- Parser Error Message: Could not load type ...
- media service 配置 问题严重 播放自己的流
- position:absolute 带来的问题!!
- 请教:框架页面,通过A框架中的选择改变B框架的内容(vb.net如何实现?)
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 90)));
int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 80)));
if (newX < 0 || newX >= width)
newX = 0;
if (newY < 0 || newY >= height)
newY = 0;
bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
}
}
}
这里在处理扭曲,distort是扭曲系数,newX,newY是扭曲以后的坐标