C#若何清除驗證碼圖片的鋸齒後果。本站提示廣大學習愛好者:(C#若何清除驗證碼圖片的鋸齒後果)文章只能為提供參考,不一定能成為您想要的結果。以下是C#若何清除驗證碼圖片的鋸齒後果正文
引言
基於生成圖片完成了一個手機號轉圖片的需求。 內容也很簡略,直接用手機號生成一個png圖片。就是為了配景通明以便其他處所挪用。 有沒有鋸齒重要依附一句代碼:g.TextRenderingHint= TextRenderingHint.AntiAlias;
生成圖片
1、有鋸齒

2、無鋸齒

生成辦法
string color = "#ff6633";
System.Drawing.Bitmap image = new System.Drawing.Bitmap(170, 35);
Graphics g = Graphics.FromImage(image);
try
{
g.TextRenderingHint= TextRenderingHint.AntiAlias; //清除鋸齒
//生成隨機生成器
Random random = new Random();
//清空圖片配景色
//g.Clear(Color.Transparent);
//繪圖片的配景樂音線
/*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);
}
*/
System.Drawing.ColorConverter colConvert = new System.Drawing.ColorConverter();
Color fontColor =(System.Drawing.Color)colConvert.ConvertFromString(color);
Font font = new System.Drawing.Font("Arial", 18, System.Drawing.FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), fontColor, fontColor,LinearGradientMode.Horizontal);
g.DrawString(phone, font, brush, 2, 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.White), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Color backColor = image.GetPixel(1, 1);
image.MakeTransparent(backColor);
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
context.Response.ClearContent();
context.Response.ContentType = "image/x-png";
context.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
參考材料
http://www.blue1000.com/bkhtml/c17/2013-03/71115.htm
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。