GVKun编程网logo

ASP.NET 使用System.Drawing 绘制随机验证码(web生成随机验证码)

10

本篇文章给大家谈谈ASP.NET使用System.Drawing绘制随机验证码,以及web生成随机验证码的知识点,同时本文还将给你拓展.NETCoreSystem.Drawing.Common中文乱码

本篇文章给大家谈谈ASP.NET 使用System.Drawing 绘制随机验证码,以及web生成随机验证码的知识点,同时本文还将给你拓展.NET Core System.Drawing.Common 中文乱码的坑、.Net Core 下使用ZKWeb.System.Drawing实现验证码功能、.Net Core 下使用ZKWeb.System.Drawing实现验证码功能(图形验证码)、.Net Core上用于代替System.Drawing的类库等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

ASP.NET 使用System.Drawing 绘制随机验证码(web生成随机验证码)

ASP.NET 使用System.Drawing 绘制随机验证码(web生成随机验证码)

总结

以上是小编为你收集整理的ASP.NET 使用System.Drawing 绘制随机验证码全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

.NET Core System.Drawing.Common 中文乱码的坑

.NET Core System.Drawing.Common 中文乱码的坑

最近在写一个汉字取点阵的程序,最开始是在win环境下运行的,没发现什么异常,然后今天把程序放在centos 下后发现英文正常,中文完全变成两位的字了,最开始是字体的原因

在把宋体等安装到centos 后发现中文出来了 但完全变了另外的字,然后使用第三方的ZKWeb.System.Drawing 运行程序,发现正常,但切换回System.Drawing.Common 就会完全不认识 或者完全变了字

比如 :我是中文
画出来后变成了

 

 

 这完全不是这个了,阅读System.Drawing.Common的源码也并没有发现其中的坑在哪里 ,跟ZKWeb.System.Drawing 也对比了下,

找到关键性代码进行对比  System.Drawing.Common 中的源码

https://github.com/dotnet/corefx/blob/master/src/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs

[DllImport(LibraryName, ExactSpelling = true, CharSet = CharSet.Unicode)]
 internal static extern int GdipDrawString(HandleRef graphics, string textString, int length, HandleRef font, ref RectangleF layoutRect, HandleRef stringFormat, HandleRef brush);

以及ZKWeb.System.Drawing中的源码

 

https://github.com/zkweb-framework/ZKWeb.System.Drawing/blob/master/src/ZKWeb.System.Drawing/System.Drawing/gdipFunctions.cs

[DllImport(GdiPlus, CharSet=CharSet.Unicode)]
 static internal extern Status GdipDrawString (IntPtr graphics, string text, int len, IntPtr font, ref RectangleF rc, IntPtr format, IntPtr brush);

进行对比 并没法发现什么区别,于是就把这个定义放到自己的程序中定义 手动调用 GdipDrawString 看看是否会有中文乱码的问题,然而发现换System.Drawing.Common中的定义或者ZKWeb.System.Drawing中的定义都可以正常显示 但切换回System.Drawing.Common 使用系统的代码

Graphics.DrawString 中文就是不行,看了下DrawString 的代码也非常简单 ,就是调用了GdipDrawString  api 绘画字符串的,其源码如下

public void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
        {
            if (brush == null)
                throw new ArgumentNullException(nameof(brush));
            if (string.IsNullOrEmpty(s))
                return;
            if (font == null)
                throw new ArgumentNullException(nameof(font));

            CheckErrorStatus(Gdip.GdipDrawString(
                new HandleRef(this, NativeGraphics),
                s,
                s.Length,
                new HandleRef(font, font.NativeFont),
                ref layoutRectangle,
                new HandleRef(format, format?.nativeFormat ?? IntPtr.Zero),
                new HandleRef(brush, brush.NativeBrush)));
        }

也没有发现什么异常,最后使用反编译查看nuget包中的 System.Drawing.Common.dll 文件,居然发现System.Drawing.Common.dll 中的GdipDrawString 居然少了CharSet 标记,判定是导致问题的所在,

于是怀疑是我没有更新到最新版本导致的,上nuget一看发现是最新的版本 4.6的版本

 

 

于是估计是微软没更新导致的,暂时解决方法就是使用ZKWeb.System.Drawing 代替或者自己把这个api自己定义并抛弃系统的Graphics.DrawString 函数 

 

原文出处:https://www.cnblogs.com/dotnet-org-cn/p/11756285.html

.Net Core 下使用ZKWeb.System.Drawing实现验证码功能

.Net Core 下使用ZKWeb.System.Drawing实现验证码功能

本文介绍.net core下用第三方zkweb.system.drawing实现验证码功能。

通过测试的系统:

Windows 8.1 64bit
Ubuntu Server 16.04 LTS 64bit
Fedora 24 64bit
CentOS 7.2 64bit

可以实现以下功能:

Open jpg, bmp, ico, png
Save jpg, bmp, ico, png
Resize image
Draw graphics with brush and pen
Open font and draw string

以上是官方给的资料。

No.1 项目引入ZKWeb.System.Drawing

NuGet引入包,不会的自己百度。

No.2 简单的验证码生成

int codeW = 80;
int codeH = 30;
int fontSize = 16;
Random rnd = new Random();
//颜色列表,用于验证码、噪线、噪点
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] font = { "Times New Roman" };
//验证码的字符集,去掉了一些容易混淆的字符
//写入Session、验证码加密
//WebHelper.WriteSession("session_verifycode", Md5Helper.MD5(chkCode.ToLower(), 16));
//创建画布
Bitmap bmp = new Bitmap(codeW, codeH);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i < 1; i++)
{
int x1 = rnd.Next(codeW);
int y1 = rnd.Next(codeH);
int x2 = rnd.Next(codeW);
int y2 = rnd.Next(codeH);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, fontSize);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0);
}
//将验证码图片写入内存流,并将其以 "image/Png" 格式输出
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
catch (Exception)
{
return null;
}
finally
{
g.Dispose();
bmp.Dispose();
}
登录后复制

No.3 发布部署运行

直接上图,不会的看这里.Net Core 之 Ubuntu 14.04 部署过程(图文详解)

.Net Core 下使用ZKWeb.System.Drawing实现验证码功能

注意:验证码Windows下生成无压力,我用的Ubuntu 14,需要安装gdi包,运行日志中会有提示。

安装方法:

Ubuntu 16.04:

apt-get install libgdiplus
cd /usr/lib
ln -s libgdiplus.so gdiplus.dll
登录后复制

Fedora 23:

dnf install libgdiplus
cd /usr/lib64/
ln -s libgdiplus.so.0 gdiplus.dll
登录后复制

CentOS 7:

yum install autoconf automake libtool
yum install freetype-devel fontconfig libXft-devel
yum install libjpeg-turbo-devel libpng-devel giflib-devel libtiff-devel libexif-devel
yum install glib2-devel cairo-devel
git clone https://github.com/mono/libgdiplus
cd libgdiplus
./autogen.sh
make
make install
cd /usr/lib64/
ln -s /usr/local/lib/libgdiplus.so gdiplus.dll
登录后复制

   

以上所述是小编给大家介绍的.Net Core 下使用ZKWeb.System.Drawing实现验证码功能(图形验证码),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHP中文网的支持!

.Net Core 下使用ZKWeb.System.Drawing实现验证码功能(图形验证码)

.Net Core 下使用ZKWeb.System.Drawing实现验证码功能(图形验证码)

本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能。

通过测试的系统:

Windows 8.1 64bit
Ubuntu Server 16.04 LTS 64bit
Fedora 24 64bit
CentOS 7.2 64bit

可以实现以下功能:

Open jpg, bmp, ico, png
Save jpg, bmp, ico, png
Resize image
Draw graphics with brush and pen
Open font and draw string

以上是官方给的资料。

No.1 项目引入ZKWeb.System.Drawing

NuGet引入包,不会的自己百度。

No.2 简单的验证码生成

int codeW = 80;
int codeH = 30;
int fontSize = 16;
Random rnd = new Random();
//颜色列表,用于验证码、噪线、噪点 
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
//字体列表,用于验证码 
string[] font = { "Times New Roman" };
//验证码的字符集,去掉了一些容易混淆的字符 
//写入Session、验证码加密
//WebHelper.WriteSession("session_verifycode", Md5Helper.MD5(chkCode.ToLower(), 16));
//创建画布
Bitmap bmp = new Bitmap(codeW, codeH);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线 
for (int i = 0; i < 1; i++)
{
int x1 = rnd.Next(codeW);
int y1 = rnd.Next(codeH);
int x2 = rnd.Next(codeW);
int y2 = rnd.Next(codeH);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串 
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, fontSize);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0);
}
//将验证码图片写入内存流,并将其以 "image/Png" 格式输出 
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
catch (Exception)
{
return null;
}
finally
{
g.Dispose();
bmp.Dispose();
}

No.3 发布部署运行

直接上图,不会的看这里.Net Core 之 Ubuntu 14.04 部署过程(图文详解)

注意:验证码Windows下生成无压力,我用的Ubuntu 14,需要安装gdi包,运行日志中会有提示。

安装方法:

Ubuntu 16.04:

apt-get install libgdiplus
cd /usr/lib
ln -s libgdiplus.so gdiplus.dll

Fedora 23:

dnf install libgdiplus
cd /usr/lib64/
ln -s libgdiplus.so.0 gdiplus.dll

CentOS 7:

yum install autoconf automake libtool
yum install freetype-devel fontconfig libXft-devel
yum install libjpeg-turbo-devel libpng-devel giflib-devel libtiff-devel libexif-devel
yum install glib2-devel cairo-devel
git clone https://github.com/mono/libgdiplus
cd libgdiplus
./autogen.sh
make
make install
cd /usr/lib64/
ln -s /usr/local/lib/libgdiplus.so gdiplus.dll

以上所述是小编给大家介绍的.Net Core 下使用ZKWeb.System.Drawing实现验证码功能(图形验证码),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

您可能感兴趣的文章:
  • [Asp.Net Core]用Blazor Server Side实现图片验证码
  • .Net Core 实现图片验证码的实现示例
  • .NET Core 2.0如何生成图片验证码完整实例
  • 使用.Net Core实现的一个图形验证码功能

.Net Core上用于代替System.Drawing的类库

.Net Core上用于代替System.Drawing的类库

目前.Net Core上没有System.Drawing这个类库,想要在.Net Core上处理图片得另辟蹊径。

微软给出了将来取代System.Drawing的方案,偏向于使用一个单独的服务端进行各种图片处理
https://github.com/dotnet/corefx/issues/2020
https://github.com/imazen/Graphics-vNext
但目前仍然没有一个可用的实现。

下面我介绍一些目前确实可用于代替System.Drawing的类库,包括我发布的ZKWeb.System.Drawing

ImageProcessor

地址: https://github.com/JimBobSquarePants/ImageProcessor/
从3.0开始支持了.Net Core。
支持的很全面,如果只用于转换缩放图片,或手动处理像素的话可以最优先考虑这个类库。
但是不支持描画验证码等描画类的功能,在将来会支持,可见https://github.com/JimBobSquarePants/ImageProcessor/issues/264。

因为作者尚未把3.0发布到nuget,安装需要添加myget的源。
如何添加myget的源可以参考https://www.myget.org/nuget。
添加后使用nuget安装ImageProcessorCore即可。

CoreCompat

地址: https://github.com/CoreCompat/CoreCompat
这个类库使用了mono的System.Drawing实现,只要安装了之前使用System.Drawing的代码完全不用修改。
也支持描画验证码等描画类的功能。

如果需要linux或osx支持,可以安装runtime.linux.CoreCompat.System.Drawingruntime.osx.10.10-x64.CoreCompat.System.Drawing

ZKWeb.System.Drawing

地址: https://github.com/zkweb-framework/zkweb.system.drawing
这个类库是我在使用CoreCompat后感到不满意而重新创建的一个类库,也是从mono的System.Drawing修改得来。

这个类库和CoreCompat的不同点如下

  • 没有使用强名称,CoreCompat为了让程序集名称一样使用了一个伪造的签名,但是导致Asp.Net和Owin等会检查签名的旧项目启动失败

  • CoreCompat的项目如果直接下载编译会出现100多个错误,大多是类型找不到的错误,我也不知道作者是怎么编译过去的
    这个项目从mono 4.6.1.13复制了所有需要的文件并修改,直接下载编译就可以通过

  • 可以使用dotnet test跑单元测试,目前通过率约为80%

  • 实际在linux上测试过并且给出了各个发行版安装libgdiplus的命令,目前已测试

    • Ubuntu Server 16.04 LTS 64bit

    • Fedora 24 64bit

    • CentOS 7.2 64bit

  • 不引用System.Drawing.Primitive,因为System.Drawing.Primitive在.Net Framework下同时引用了原来的System.Drawing,有可能导致编译时类型冲突(实测只有警告)

ZKWeb.System.Drawing这个类库在有更好的代替方案之前将会一直维护,如果使用中遇到问题或错误欢迎到项目地址提出issue。

原文地址:http://www.cnblogs.com/zkweb/p/5999205.html


.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注

本文分享自微信公众号 - dotNET跨平台(opendotnet)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

今天的关于ASP.NET 使用System.Drawing 绘制随机验证码web生成随机验证码的分享已经结束,谢谢您的关注,如果想了解更多关于.NET Core System.Drawing.Common 中文乱码的坑、.Net Core 下使用ZKWeb.System.Drawing实现验证码功能、.Net Core 下使用ZKWeb.System.Drawing实现验证码功能(图形验证码)、.Net Core上用于代替System.Drawing的类库的相关知识,请在本站进行查询。

本文标签:

上一篇asp.net-mvc – SQL Azure得到一个错误’已经有一个打开的DataReader与此命令..’关联,即使设置’MultipleActiveResultSets = True’

下一篇asp.net-mvc – 任何潜在的安全风险设置,打开relaxedUrlToFileSystemMapping以允许以“ ”结尾的网址