简体   繁体   English

Graphics.DrawString中的字符更改

[英]Characters changing in Graphics.DrawString

I have a dll written in C#. 我有一个用C#编写的dll。 One of the methods takes a parameter string txt . 其中一种方法采用参数string txt The dll is being called from a classic asp page. 该dll是从经典的ASP页面中调用的。 At the time of calling, the text contains the character é which is obtained by holding down ALT and typing 130 on the number pad or by holding down ALT-GR and hitting e . 在呼叫时,文本包含字符é ,可通过按住ALT并在数字键盘上键入130或按住ALT-GR并按e来获得

The DrawString method is outputting it wrong. DrawString方法输出错误。 I realize there is a character set/font issue, but I don't know how to get round the problem. 我意识到存在字符集/字体问题,但是我不知道如何解决该问题。

What I get on a British keyboard and localization using Arial font is a capital A with a tilde above it followed by lowercase c in a circle. 我在英式键盘上使用Arial字体进行的本地化是大写字母A,上方带有波浪号,后跟一个小写的c。

The individual entering the text may be using any keyboard layout, may have any localization on their PC, but they will all be entering it via our web page. 输入文本的个人可以使用任何键盘布局,可以在PC上进行任何本地化,但是他们都将通过我们的网页进行输入。

What do I need to do to make things compatible or to convert? 我需要做些什么来使事情兼容或转换?

The relevant bit of code is: 相关的代码位是:

Bitmap image = new Bitmap(strFilePath);

Graphics m_graphics;
m_graphics = Graphics.FromImage(image);

Font fnt = new Font("Arial", 12.0f);

StringFormat strFormatter = new StringFormat();

SolidBrush txtBrush = new SolidBrush(Color.FromArgb(127, Color.FromArgb(int.Parse("AB12CD", NumberStyles.HexNumber))));

m_graphics.DrawString("é", fnt, txtBrush, new Point(200, 200), strFormatter);

As your string of text is coming from a web page, it could be URL encoded. 由于您的文本字符串来自网页,因此可能是经过URL编码的。 Try decoding it first, and making sure the result is UTF8: 尝试先对其进行解码,并确保结果为UTF8:

string strDecoded = HttpUtility.UrlDecode(strEncoded, Encoding.UTF8)

m_graphics.DrawString(strEncoded, fnt, txtBrush, new Point(200, 200), strFormatter);

é is encoded as U+00E9 in Unicode, which results in UTF-8 0xC3 0xA9 . é以Unicode编码为U+00E9 ,结果为UTF-8 0xC3 0xA9

U+00C3 is à , and U+00A9 is © ( source ). U+00C3à ,而U+00A9© )。

So you clearly have an encoding problem, and the relevant code is not the one you posted, but the code that receives the string from the web request and passes it through the page (code-behind?) into your DLL. 因此,您显然有一个编码问题,而相关的代码不是您发布的代码,而是从Web请求接收字符串并将其通过页面(代码隐藏?)传递到DLL的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM