本文整理汇总了C#中System.Drawing.Drawing2D.LinearGradientBrush类的典型用法代码示例。如果您正苦于以下问题:C# System.Drawing.Drawing2D.LinearGradientBrush类的具体用法?C# System.Drawing.Drawing2D.LinearGradientBrush怎么用?C# System.Drawing.Drawing2D.LinearGradientBrush使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
System.Drawing.Drawing2D.LinearGradientBrush类属于命名空间,在下文中一共展示了System.Drawing.Drawing2D.LinearGradientBrush类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCheckCode
/// <summary>
/// 作者:Raymond
/// 时间:2013.11.2
/// 描述:获取验证码
/// --------------------------------
/// 更新:Vincen
/// 时间:2013.11.14 PM
/// </summary>
public static void GetCheckCode()
{
string checkCode = GenerateCheckCode();
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
HttpContext.Current.Response.Cookies.Add(new HttpCookie(Global.CheckCodeKey, checkCode));
var image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//清空图片背景色
g.Clear(Color.White);
var font = new Font("Verdana", 11, (FontStyle.Regular | FontStyle.Italic));
var 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);
var ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
示例2: DrawBackground
public override void DrawBackground(Graphics g, Point center, bool active)
{
Rectangle r = new Rectangle(center.X - Dimensions.Width / 2, center.Y - Dimensions.Height / 2, Dimensions.Width, Dimensions.Height);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(r, active?BlueGrad1:GrayGrad1, active?BlueGrad2:GrayGrad2, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
Pen p = new Pen(active?(RefBorder):GrayBorder, linewidth);
RoundRect(g, p, b, r);
}
示例3: GetCaptchaImage
public byte[] GetCaptchaImage(string checkCode)
{
Bitmap image = new Bitmap(Convert.ToInt32(Math.Ceiling((decimal)(checkCode.Length * 15))), 25);
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
g.Clear(Color.AliceBlue);
Font font = new Font("Comic Sans MS", 14, FontStyle.Bold);
string str = "";
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);
for (int i = 0; i < checkCode.Length; i++)
{
str = str + checkCode.Substring(i, 1);
}
g.DrawString(str, font, new SolidBrush(Color.Blue), 0, 0);
g.Flush();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
finally
{
g.Dispose();
image.Dispose();
}
}
示例4: DrawTextIntoFixedSizeBitmap
public static Bitmap DrawTextIntoFixedSizeBitmap(string text, Size size, Font dp, Color c, Color b, float backscale = 1.0F)
{
Bitmap img = new Bitmap(size.Width, size.Height);
using (Graphics dgr = Graphics.FromImage(img))
{
if (b != Color.Transparent && text.Length > 0)
{
SizeF sizef = dgr.MeasureString(text, dp);
Rectangle backarea = new Rectangle(0, 0, (int)(sizef.Width + 1), (int)(sizef.Height + 1));
using (Brush bb = new System.Drawing.Drawing2D.LinearGradientBrush(backarea, b, ButtonExt.Multiply(b, backscale), 90))
dgr.FillRectangle(bb, backarea);
dgr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // only if filled
}
using (Brush textb = new SolidBrush(c))
dgr.DrawString(text, dp, textb, 0, 0);
dgr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
return img;
}
}
示例5: DrawTextIntoAutoSizedBitmap
// you set the maxsize, which the text is clipped to. if b != Transparent, a back box is drawn.
public static Bitmap DrawTextIntoAutoSizedBitmap(string text, Size maxsize, Font dp, Color c, Color b, float backscale = 1.0F)
{
Bitmap t = new Bitmap(1, 1);
using (Graphics bgr = Graphics.FromImage(t))
{
SizeF sizef = bgr.MeasureString(text, dp);
int width = Math.Min((int)(sizef.Width + 1), maxsize.Width);
int height = Math.Min((int)(sizef.Height + 1), maxsize.Height);
Bitmap img = new Bitmap(width, height);
using (Graphics dgr = Graphics.FromImage(img))
{
if (b != Color.Transparent && text.Length > 0)
{
Rectangle backarea = new Rectangle(0, 0, img.Width, img.Height);
using (Brush bb = new System.Drawing.Drawing2D.LinearGradientBrush(backarea, b, ButtonExt.Multiply(b, backscale), 90))
dgr.FillRectangle(bb, backarea);
dgr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // only worth doing this if we have filled it.. if transparent, antialias does not work
}
using (Brush textb = new SolidBrush(c))
dgr.DrawString(text, dp, textb, 0, 0);
dgr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
return img;
}
}
}
示例6: DrawBinaryImage
//BinaryWrite方法将一个二进制字符串写入HTTP输出流
//public void BinaryWrite(byte[] buffer)
public void DrawBinaryImage()
{
//要绘制的字符串
string word = "5142";
//设定画布大小,Math.Ceiling向上取整
int width = int.Parse(Math.Ceiling(word.Length * 20.5).ToString());
int height = 22;
//创建一个指定大小的画布
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
//创建一个指定的GDI+绘图图面
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
try
{
//用白色填充图面
g.Clear(System.Drawing.Color.White);
//绘制背景噪音线
Random r = new Random();
int x1, x2, y1, y2;
for (int i = 0; i < 2; i++)
{
x1 = r.Next(image.Width);
x2 = r.Next(image.Width);
y1 = r.Next(image.Height);
y2 = r.Next(image.Height);
g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black), x1, y1, x2, y2);
}
//绘制文字
System.Drawing.Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush
(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 5.2f, true);
g.DrawString(word, font, brush, 2, 2);
//绘制前景噪点
int x, y;
for (int i = 0; i < 100; i++)
{
x = r.Next(image.Width);
y = r.Next(image.Height);
image.SetPixel(x, y, System.Drawing.Color.FromArgb(r.Next()));
}
//绘制边框
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
catch (Exception)
{
g.Dispose();
image.Dispose();
}
}
示例7: create_hue_bitmap
public static System.Drawing.Bitmap create_hue_bitmap(int width, int height)
{
var bitmap = new System.Drawing.Bitmap(width, height);
using (var gfx = System.Drawing.Graphics.FromImage(bitmap))
{
var colorblend = new System.Drawing.Drawing2D.ColorBlend();
const int num_steps = 34;
var range_steps = EnumerableUtil.RangeSteps(0.0, 1.0, num_steps);
colorblend.Colors = new System.Drawing.Color[num_steps];
colorblend.Positions = new float[num_steps];
double _sat = 1.0;
double _val = 1.0;
var colors = range_steps.Select(x => VisioAutomation.UI.ColorUtil.HSVToSystemDrawingColor(x, _sat, _val));
var positions = range_steps.Select(x => (float) x);
EnumerableUtil.FillArray( colorblend.Colors, colors );
EnumerableUtil.FillArray(colorblend.Positions, positions);
using (var brush_rainbow = new System.Drawing.Drawing2D.LinearGradientBrush(
new System.Drawing.Point(0, 0),
new System.Drawing.Point(bitmap.Width, 0),
System.Drawing.Color.Black,
System.Drawing.Color.White))
{
brush_rainbow.InterpolationColors = colorblend;
gfx.FillRectangle(brush_rainbow, 0, 0, bitmap.Width, bitmap.Height);
}
}
return bitmap;
}
示例8: GetValidateImg
public static System.IO.MemoryStream GetValidateImg(out string code, string bgImg = "/Images/vcodebg.png")
{
code = GetValidateCode();
Random rnd = new Random();
System.Drawing.Bitmap img = new System.Drawing.Bitmap((int)Math.Ceiling((code.Length * 17.2)), 28);
System.Drawing.Image bg = System.Drawing.Bitmap.FromFile(HttpContext.Current.Server.MapPath(bgImg));
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
System.Drawing.Font font = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic));
System.Drawing.Font fontbg = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true);
g.DrawImage(bg, 0, 0, new System.Drawing.Rectangle(rnd.Next(bg.Width - img.Width), rnd.Next(bg.Height - img.Height), img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);
g.DrawString(code, fontbg, System.Drawing.Brushes.White, 0, 1);
g.DrawString(code, font, System.Drawing.Brushes.Green, 0, 1);//字颜色
//画图片的背景噪音线
int x = img.Width;
int y1 = rnd.Next(5, img.Height);
int y2 = rnd.Next(5, img.Height);
g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Green, 2), 1, y1, x - 2, y2);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Transparent), 0, 10, img.Width - 1, img.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms;
}
示例9: Draw3ColorBar
public static void Draw3ColorBar(System.Drawing.Graphics dc, System.Drawing.RectangleF r, System.Windows.Forms.Orientation orientation, System.Drawing.Color c1, System.Drawing.Color c2,
System.Drawing.Color c3)
{
// to draw a 3 color bar 2 gradient brushes are needed
// one from c1 - c2 and c2 - c3
var lr1 = r;
var lr2 = r;
float angle = 0;
if (orientation == System.Windows.Forms.Orientation.Vertical)
{
angle = 270;
lr1.Height = lr1.Height/2;
lr2.Height = r.Height - lr1.Height;
lr2.Y += lr1.Height;
}
if (orientation == System.Windows.Forms.Orientation.Horizontal)
{
angle = 0;
lr1.Width = lr1.Width/2;
lr2.Width = r.Width - lr1.Width;
lr1.X = lr2.Right;
}
if (lr1.Height > 0 && lr1.Width > 0)
{
using (System.Drawing.Drawing2D.LinearGradientBrush lb2 = new System.Drawing.Drawing2D.LinearGradientBrush(lr2, c1, c2, angle, false), lb1 = new System.Drawing.Drawing2D.LinearGradientBrush(lr1, c2, c3, angle, false) )
{
dc.FillRectangle(lb1, lr1);
dc.FillRectangle(lb2, lr2);
}
}
// with some sizes the first pixel in the gradient rectangle shows the opposite color
// this is a workaround for that problem
if (orientation == System.Windows.Forms.Orientation.Vertical)
{
using (System.Drawing.Pen pc2 = new System.Drawing.Pen(c2, 1), pc3 = new System.Drawing.Pen(c3, 1))
{
dc.DrawLine(pc3, lr1.Left, lr1.Top, lr1.Right - 1, lr1.Top);
dc.DrawLine(pc2, lr2.Left, lr2.Top, lr2.Right - 1, lr2.Top);
}
}
if (orientation == System.Windows.Forms.Orientation.Horizontal)
{
using (System.Drawing.Pen pc1 = new System.Drawing.Pen(c1, 1), pc2 = new System.Drawing.Pen(c2, 1), pc3 = new System.Drawing.Pen(c3, 1))
{
dc.DrawLine(pc1, lr2.Left, lr2.Top, lr2.Left, lr2.Bottom - 1);
dc.DrawLine(pc2, lr2.Right, lr2.Top, lr2.Right, lr2.Bottom - 1);
dc.DrawLine(pc3, lr1.Right, lr1.Top, lr1.Right, lr1.Bottom - 1);
}
}
}
示例10: CreateCheckCodeImage
/// <summary>
/// 根据验证码生成图片
/// </summary>
/// <param name="checkCode">验证码</param>
private static byte[] CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return null;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 16.0)), 30);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.LightGray);
//画图片的背景噪音线
for (int i = 0; i < 30; 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.IndianRed), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Verdana", 14, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Strikeout));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Black, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 50; i++)
{
int x = 0; random.Next(image.Width);
int y = 0; random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.LightGray), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
//输出图片流
return ms.ToArray();
//HttpContext.Current.Response.ClearContent();
//HttpContext.Current.Response.ContentType = "image/Gif";
//HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
示例11: CreateCheckCodeImage
private void CreateCheckCodeImage(string checkCode)
{
if(checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(54, 20);
Graphics g = Graphics.FromImage(image);
try
{
//�������������
Random random = new Random();
//���ͼƬ����ɫ
g.Clear(Color.White);
//��ͼƬ�ı���������
// for(int i=0; i<10; 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,0.5f), x1, y1, x2, y2);
// }
Font font = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold);
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.Substring(0,1), font, brush, 2, 2);
g.DrawString(checkCode.Substring(1,1), font, brush, 14, 2);
g.DrawString(checkCode.Substring(2,1), font, brush, 26, 2);
g.DrawString(checkCode.Substring(3,1), font, brush, 38, 2);
//��ͼƬ��ǰ�������
for(int i=0; i<50; 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, image.Height);
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();
}
}
示例12: CreateCheckCodeImage
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
//(int)Math.Ceiling((checkCode.Length * 12.5)),22
System.Drawing.Bitmap image = new System.Drawing.Bitmap(68, 28);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 5; i++)
{
int x1 = random.Next(image.Width + 10);
int x2 = random.Next(image.Width + 10);
int y1 = random.Next(image.Height + 10);
int y2 = random.Next(image.Height + 10);
Pen p = new Pen(Color.Gray, 4);
g.DrawLine(p, x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 18, (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, System.Drawing.Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 1, 1);
//画图片的前景噪音点
for (int i = 0; i < 200; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, System.Drawing.Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.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);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/jpg";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
示例13: CreateCheckCodeImage
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 * 12.5)), 20);
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);
}
Font font = new System.Drawing.Font("Arial", 12, (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.Png);
Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
PublicMethod.SafeResponseEnd(Response, Request.RawUrl);
}
finally
{
g.Dispose();
image.Dispose();
}
}
示例14: CreateCheckCodeImage
private static byte[] CreateCheckCodeImage(string checkCode)
{
byte[] tmpImg = null;
if (checkCode == null || checkCode.Trim() == String.Empty)
return tmpImg;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.FromArgb(215, 215, 215));
//画图片的背景噪音线
for (int i = 0; i < 2; 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));
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);
tmpImg = ms.ToArray();
}
finally
{
g.Dispose();
image.Dispose();
}
return tmpImg;
}
示例15: img_CheckCode
protected void img_CheckCode(string checkcode, HttpContext context)
{
System.Drawing.Bitmap img = new Bitmap((int)(checkcode.Length * 12.5), 22);
Graphics gh = Graphics.FromImage(img);
try
{
Random rd = new Random();
gh.Clear(Color.White);
//噪音线画图
for (int i = 0; i < 2; i++)
{
int x1 = rd.Next(img.Width);
int y1 = rd.Next(img.Width);
int x2 = rd.Next(img.Height);
int y2 = rd.Next(img.Height);
gh.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
//添上验证码
Font font = new Font("Arial", 12, System.Drawing.FontStyle.Bold);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkRed, 1.2f, true);
gh.DrawString(checkcode, font, brush, 2, 2);
//噪音点
for (int i = 0; i < 100; i++)
{
int x = rd.Next(img.Width);
int y = rd.Next(img.Height);
img.SetPixel(x, y, Color.FromArgb(rd.Next()));
}
//画图片的边框
gh.DrawRectangle(new Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1);
//写入流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
}
catch
{
context.Response.Write("<script>alert('制图出错');</script>");
}
finally
{
gh.Dispose();
img.Dispose();
}
}