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

  • 相关阅读:
    数据库:数据操作-数据的增删改
    数据库:表操作-完整性约束
    数据库:表操作-枚举类型与集合类型
    数据库:表操作-字符串类型
    数据库:表操作-数据类型(日期类型)
    数据库:表操作-数据类型(数值类型)
    python并发编程:IO模型比较分析
    java学习(二)--excel导出
    格式化字段
    myeclipse的常用快捷键
  • 原文地址:https://www.cnblogs.com/dotnet-org-cn/p/11756285.html
Copyright © 2020-2023  润新知