C#如何消除验证码图片的锯齿效果
作者:garfieldzf 时间:2023-11-05 04:49:14
引言
基于生成图片实现了一个手机号转图片的需求。 内容也很简单,直接用手机号生成一个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
来源:http://www.cnblogs.com/sword-successful/archive/2016/09/18/5880866.html
标签:C#,锯齿,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java连接SQL Server数据库的超详细教程
2023-04-05 21:46:22
![](https://img.aspxhome.com/file/2023/5/63095_0s.png)
c#中CAD文件读取实例
2023-07-23 19:37:59
Java正确使用访问修饰符的姿势
2021-10-11 09:52:50
![](https://img.aspxhome.com/file/2023/5/76595_0s.png)
关于AndroidStudio R文件莫名其妙缺失的快速解决方法
2023-02-24 21:59:06
![](https://img.aspxhome.com/file/2023/2/126752_0s.jpg)
Android编程实现ListView滚动提示等待框功能示例
2022-11-15 13:50:15
SpringCloud使用Feign实现服务调用
2021-11-10 05:19:20
![](https://img.aspxhome.com/file/2023/6/132146_0s.png)
Android 按后退键退出Android程序的实现方法
2023-06-16 20:08:02
详解RestTemplate的三种使用方式
2023-06-07 16:30:45
解析maven的用法和几个常用的命令(推荐)
2022-04-16 23:31:15
![](https://img.aspxhome.com/file/2023/4/87174_0s.jpg)
详解C语言实现猜数字游戏
2023-11-03 04:58:51
![](https://img.aspxhome.com/file/2023/7/105177_0s.jpg)
Springboot基础学习之初识SpringBoot
2022-08-18 08:48:59
![](https://img.aspxhome.com/file/2023/8/75408_0s.png)
SpringMVC入门实例
2023-02-04 12:43:48
![](https://img.aspxhome.com/file/2023/8/69348_0s.gif)
C# 重写ComboBox实现下拉任意组件的方法
2022-01-24 03:07:29
Java基础之二叉搜索树的基本操作
2023-07-08 10:07:07
![](https://img.aspxhome.com/file/2023/9/61019_0s.png)
Android开发人脸识别登录功能
2022-03-02 14:16:12
![](https://img.aspxhome.com/file/2023/7/86757_0s.png)
Android图片缓存之初识Glide(三)
2022-02-18 12:05:21
SpringBoot控制配置类加载顺序方式
2022-08-18 04:31:15
![](https://img.aspxhome.com/file/2023/8/129228_0s.png)
通俗易通讲解Android蓝牙键值适配
2022-06-19 07:50:34
![](https://img.aspxhome.com/file/2023/0/120100_0s.jpg)
泛谈Java中的不可变数据结构
2022-02-18 00:12:54
Java经验点滴:类注释文档编写方法
2023-11-06 03:50:11