C#实现截取验证码图片
作者:老狗子 时间:2023-08-04 04:22:56
本文实例为大家分享了C#截取验证码图片的具体代码,供大家参考,具体内容如下
使用Graphics类中的DrawImage方法,这个方法有30种重载方式,这里只介绍一种,也是我认为最直观的一种,代码如下:
using System.Drawing;
namespace kq.Utils
{
public static class CommonTools
{
public static Bitmap getVerifyCode(Bitmap srcBmp, Rectangle rectangle)
{
//初始化一个bmp对象,90代表图片的宽度,37代表高度
Bitmap bmp = new Bitmap(90, 37);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(srcBmp, 0, 0, rectangle, GraphicsUnit.Pixel);
return bmp;
}
}
}
其中g.DrawImage方法中第一个参数代表被截取的原图,第二第三个参数(0,0)代表在bmp中画的起点xy坐标,第四个参数rectangle代表从srcBmp中截取的区域,最后一个参数GraphicsUnit.Pixel表示以上代表距离以及区域的参数的单位,Pixel代表像素。
以下是使用方式:
using kq.Utils;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Drawing;
namespace kq
{
class Program
{
static void Main(string[] args)
{
try
{
string screenImg = @"d:\screenImg.png";
Bitmap fromBmp = new Bitmap(screenImg);
Rectangle section1 = new Rectangle(936, 523, 90, 37);
Bitmap bmp = CommonTools.getVerifyCode(fromBmp, section1);
bmp.Save(@"d:\验证码.bmp");
}
catch (System.Exception e)
{
System.Console.WriteLine(e.Message);
}
}
}
}
假设我们要截取一张图片中的验证码部分,代码中的(936,523)代表原图中验证码左上角的坐标,(90,37)分别表示验证码的长和高,以上单位都是像素,如下图:
最后截取出来的结果如下图:
来源:https://www.cnblogs.com/lzttttt/archive/2018/12/12/10107881.html
标签:C#,截取,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java泛型的简单实例
2023-11-27 01:03:38
Android自定义View实现星星评分效果
2023-10-27 23:10:42
![](https://img.aspxhome.com/file/2023/0/137740_0s.png)
Android ListView适配器(Adapter)优化方法详解
2023-08-09 21:44:32
![](https://img.aspxhome.com/file/2023/6/97856_0s.jpg)
spring mvc4中相关注解的详细讲解教程
2021-10-11 23:21:17
SpringBoot使用WebSocket的方法实例详解
2022-12-26 03:19:25
Java设计模式之桥接模式的示例详解
2023-10-03 21:56:00
![](https://img.aspxhome.com/file/2023/9/113179_0s.jpg)
Java并发之不可思议的死循环详解
2023-10-20 03:11:34
深入理解SpringMVC中央调度器DispatcherServlet
2023-03-11 08:54:48
![](https://img.aspxhome.com/file/2023/4/119184_0s.jpg)
Spring Security 实现短信验证码登录功能
2022-11-02 19:39:30
![](https://img.aspxhome.com/file/2023/6/72326_0s.jpg)
android不同activity之间共享数据解决方法
2023-12-22 04:30:45
Android使用WebView实现文件下载功能
2022-03-01 09:41:44
![](https://img.aspxhome.com/file/2023/1/139501_0s.png)
SpringBoot在项目中访问静态资源步骤分析
2022-12-13 08:04:31
![](https://img.aspxhome.com/file/2023/2/110932_0s.png)
Spring 事务事件监控及实现原理解析
2023-12-24 04:20:20
详解WPF中的隧道路由和冒泡路由事件
2023-03-01 07:59:49
![](https://img.aspxhome.com/file/2023/8/68068_0s.png)
C#多线程之线程控制详解
2022-02-04 17:51:44
![](https://img.aspxhome.com/file/2023/8/111078_0s.png)
深入了解Java中String、Char和Int之间的相互转换
2022-09-14 10:18:54
![](https://img.aspxhome.com/file/2023/2/64382_0s.jpg)
详解SpringCloud微服务之Rest
2023-10-20 00:49:36
Android开发实现Launcher3应用列表修改透明背景的方法
2023-09-28 08:45:27
简单学习C#中的泛型方法使用
2022-11-06 19:37:27
一些比较实用的 Android adb 命令分享
2023-12-22 12:39:41