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

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

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

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

 

 

 这彻底不是这个了,阅读System.Drawing.Common的源码也并无发现其中的坑在哪里 ,跟ZKWeb.System.Drawing 也对比了下,centos

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

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.csthis

[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 使用系统的代码spa

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

 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 函数 

相关文章
相关标签/搜索