最近在写一个汉字取点阵的程序,最开始是在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 函数

.NET Core System.Drawing.Common 中文乱码的坑的更多相关文章

  1. .Net Core 使用 System.Drawing.Common 部署到CentOS上遇到的问题

    一开始报这个错误:Unable to load shared library 'libdl' 找到libdl安装位置是/usr/lib64: #locate libdl /usr/lib64/libd ...

  2. Asp.Net Core使用System.Drawing.Common部署到docker报错问题

    Asp.Net Core 2.1发布后,正式支持System.Drawing.Common绘图了,可以用来做一些图片验证码之类的功能.但是把网站部署到docker容器里运行会遇到很多问题,也是非常闹心 ...

  3. .Net Core 使用 System.Drawing.Common 在CentOS下报错

    .Net Core控制台项目,添加了 System.Drawing.Common 引用 #locate libdl /usr/lib64/libdl-2.17.so /usr/lib64/libdl. ...

  4. Linux/Docker 中使用 System.Drawing.Common 踩坑小计

    前言 在项目迁移到 .net core 上面后,我们可以使用 System.Drawing.Common 组件来操作 Image,Bitmap 类型,实现生成验证码.二维码,图片操作等功能.Syste ...

  5. 在linux 或docker中使用 system.drawing.common

    在dockerfile 中添加 FROM microsoft/dotnet:2.1-aspnetcore-runtime RUN apt-get update RUN apt-get install ...

  6. Install-Package:QRCoder已拥有为System.Drawing.Common定义的依赖项

    error_log PM> Install-Package QRCoder -Version 1.3.3 Install-Package : "QRCoder"已拥有为&qu ...

  7. SpringMVC中文乱码踩坑

    问题 使用SpringMVC在返回一个字符串时发生了中文乱码问题.produces属性无效 @RequestMapping(value = "/nihao", produces = ...

  8. asp.net core 2.1 容器中使用 System.Drawing.Common 的问题

  9. Zend Studio导致PHP插入数据库中文乱码【坑了个爹】

    用PHP往数据库里面插入数据,在执行INSERT语句前已经执行过 SET NAMES UTF8命令,MySql数据库的编码也确定是UTF8,然而插入中文的结果还是乱码. 找来找去,最后发现原来是用的I ...

随机推荐

  1. [Leetcode] 第313题 超级丑数

    一.题目描述 编写一段程序来查找第 n 个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7,13, ...

  2. JavaScript之深入函数(二)

    上一篇我们主要讲解了函数的执行过程和原理,本篇我们将介绍函数的另外两个特殊表现:闭包和立即执行函数. 一 闭包 1,  闭包的形成 之前我们提到,函数执行完毕,马上就会销毁自己的AO对象.但是如果遇到 ...

  3. SpringMVC 图片上传虚拟目录

    可以直接在tomcat的server.xml文件中进行设置,位置在Host中 添加内容为:<Context docBase="G:\JAVAtest\temp" path=& ...

  4. Ubuntu18.04直接安装python3.7或者升级自带的python3.6版本之后导致终端无法打开的解决办法

    安装ptyhon3.7 sudo apt-get update sudo apt-get install python3.7 安装成后的目录在/usr/bin/python3.7,同时将其设置成默认 ...

  5. redis分布式锁-自动超时锁(在用)

    1.加锁代码结构 2.解锁代码结构 3.java实例 4.测试类 5.测试日志 加锁代码结构 def acquire_lock_with_timeout(conn,lockname,acquire_t ...

  6. linux 安装docker

    1.安装环境 此处在Centos7进行安装,可以使用以下命令查看CentOS版本 lsb_release -a 在 CentOS 7安装docker要求系统为64位.系统内核版本为 3.10 以上,可 ...

  7. java8 两个时间比较

    比如在15:30:30之前: LocalTime.now().isBefore(LocalTime.of(15, 30,30)) 或15:30:30之后 LocalTime.now().isAfter ...

  8. 【ADO.NET基础知识】SqlConnection、command、DataSet 、DataTable、dataAdapter

    1.使用Connection连接数据库的步骤: (1).添加命名空间 System.Data.SqlClient(注意:初学者经常会忘记) (2)定义连接字符串.连接SQL Server 数据库时: ...

  9. php常用函数(第一版)

    1.array_slice 作用:数组分页函数 案例:$output  =  array_slice ( $input , - 2 ,  1 ); 2.array_column 作用:数组根据值取出一 ...

  10. 用哈希算法的思想解决排序和字符串去重问题,时间复杂度为O(N)

    第一个题目: int a[] = {12,13,12,13,19,18,15,12,15,16,17},要求对数组a进行排序,要求时间复杂度为O(N) 我们所知道的常规排序中,最优的解法也就是O(N* ...