zoukankan      html  css  js  c++  java
  • 读文件出现乱码解决方案

      1 class CMMBUtility
      2    {
      3        private CMMBUtility()  { }
      4
      5        /// <summary>
      6        /// 
      7        /// </summary>
      8        /// <param name="b"></param>
      9        /// <param name="txt"></param>
     10        /// <param name="TextFont"></param>
     11        /// <param name="TextSize"></param>
     12        /// <param name="c"></param>
     13        /// <param name="x"></param>
     14        /// <param name="y"></param>
     15        /// <param name="width"></param>
     16        /// <param name="height"></param>
     17        /// <returns></returns>

     18        public static Bitmap WriteTxtOnBitmap(Bitmap b, string txt, int TextSize, Color c, int x, int y, int width, int height)
     19        {
     20            if (b == null)
     21            {
     22                return null;
     23            }

     24            Graphics g = Graphics.FromImage(b);
     25            Font drawFont = new Font("宋体", (float)TextSize, FontStyle.Regular);
     26            SolidBrush drawBrush = new SolidBrush(c);
     27            RectangleF drawRect = new RectangleF(x, y, width, height);
     28            g.DrawString(txt, drawFont, drawBrush, drawRect);
     29            g.Dispose();
     30
     31            return b;
     32        }

     33
     34        public static String GetDataPath(Form currentForm)
     35        {
     36            String pathStr = currentForm.GetType().Assembly.ManifestModule.FullyQualifiedName;
     37            pathStr = pathStr.Substring(0, pathStr.LastIndexOf('\\'));
     38            return pathStr;
     39        }

     40
     41        /// <summary>
     42        /// 取得一个文本文件的编码方式。如果无法在文件头部找到有效的前导符,Encoding.Default将被返回。
     43        /// </summary>
     44        /// <param name="fileName">文件名。</param>
     45        /// <returns></returns>

     46        //public static Encoding GetEncoding(string fileName)
     47        //{
     48        //    return GetEncoding(fileName, Encoding.Default);
     49        //}
     50        /// <summary>
     51        /// 取得一个文本文件流的编码方式。
     52        /// </summary>
     53        /// <param name="stream">文本文件流。</param>
     54        /// <returns></returns>

     55        public static Encoding GetEncoding(FileStream stream)
     56        {
     57            return GetEncoding(stream, Encoding.Default);
     58        }

     59        /// <summary>
     60        /// 取得一个文本文件的编码方式。
     61        /// </summary>
     62        /// <param name="fileName">文件名。</param>
     63        /// <param name="defaultEncoding">默认编码方式。当该方法无法从文件的头部取得有效的前导符时,将返回该编码方式。</param>
     64        /// <returns></returns>

     65        public static Encoding GetEncoding(string fileName, Encoding defaultEncoding)
     66        {
     67            FileStream fs = new FileStream(fileName, FileMode.Open);
     68            Encoding targetEncoding = GetEncoding(fs, defaultEncoding);
     69            fs.Close();
     70            return targetEncoding;
     71        }

     72        /// <summary>
     73        /// 取得一个文本文件流的编码方式。
     74        /// </summary>
     75        /// <param name="stream">文本文件流。</param>
     76        /// <param name="defaultEncoding">默认编码方式。当该方法无法从文件的头部取得有效的前导符时,将返回该编码方式。</param>
     77        /// <returns></returns>

     78        public static Encoding GetEncoding(FileStream stream, Encoding defaultEncoding)
     79        {
     80            Encoding targetEncoding = defaultEncoding;
     81            if (stream != null && stream.Length >= 2)
     82            {
     83                //保存文件流的前4个字节
     84                byte byte1 = 0;
     85                byte byte2 = 0;
     86                byte byte3 = 0;
     87              //  byte byte4 = 0;
     88                //保存当前Seek位置
     89                long origPos = stream.Seek(0, SeekOrigin.Begin);
     90                stream.Seek(0, SeekOrigin.Begin);
     91
     92                int nByte = stream.ReadByte();
     93                byte1 = Convert.ToByte(nByte);
     94                byte2 = Convert.ToByte(stream.ReadByte());
     95                if (stream.Length >= 3)
     96                {
     97                    byte3 = Convert.ToByte(stream.ReadByte());
     98                }

     99                //if (stream.Length >= 4)
    100                //{
    101                //    byte4 = Convert.ToByte(stream.ReadByte());
    102                //}
    103                //根据文件流的前4个字节判断Encoding
    104                //Unicode {0xFF, 0xFE};
    105                //BE-Unicode {0xFE, 0xFF};
    106                //UTF8 = {0xEF, 0xBB, 0xBF};
    107                if (byte1 == 0xFE && byte2 == 0xFF)//UnicodeBe
    108                {
    109                    targetEncoding = Encoding.BigEndianUnicode;
    110                }

    111                if (byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF)//Unicode
    112                {
    113                    targetEncoding = Encoding.Unicode;
    114                }

    115                if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF)//UTF8
    116                {
    117                    targetEncoding = Encoding.UTF8;
    118                }

    119                //恢复Seek位置      
    120                stream.Seek(origPos, SeekOrigin.Begin);
    121            }

    122            return targetEncoding;
    123        }

    124    }
    摘自:cnblogs
  • 相关阅读:
    C/C++ 编写一个通用的Makefile 来编译.c .cpp 或混编
    C/C++ 定义接口文件格式
    MySql存储过程例子1
    项目所遇问题
    linux下编译C++程序无法链接Mysql的问题
    linux 同步时间 调试core内核
    CentOS安装与更新git
    03 js基本数据类型、 js运算符1
    02 js运行原理 、js开发工具介绍 、js程序入门、 js基本语法
    01 js基本介绍
  • 原文地址:https://www.cnblogs.com/wuming/p/1410866.html
Copyright © 2011-2022 走看看