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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124
