如果操作系统低于Win10且使用从右到左书写的语言,则GraphicsPath AddString对字体的支持不足。

11

我需要在我的WPF应用程序中从字符串生成一个图像并显示它。这是我的代码:

// Create Image and generate string on it
System.Windows.Controls.Image img = new System.Windows.Controls.Image();


// This is the font that I need to render
Font DefaultFont = new Font("Oybab Tuz", 40f, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel);

// Generate image from string
GraphicsPath graphicsPath = new GraphicsPath();

// As you see, the string include Right to left character, also include some other character in the middle
graphicsPath.AddString("سىناق123456789سىناق", DefaultFont.FontFamily, (int)DefaultFont.Style, DefaultFont.Size * 96f / 72f, new PointF(0f, 0f), StringFormat.GenericDefault);

// Turn the string to the image
RectangleF bounds = graphicsPath.GetBounds();

Bitmap bitmap = new Bitmap((int)(bounds.Width + bounds.X), (int)(bounds.Height + bounds.Y));
Graphics graphic = Graphics.FromImage(bitmap);

graphic.FillPath(new SolidBrush(System.Drawing.Color.FromArgb(0x33, 0x00, 0xFF)), graphicsPath);
graphicsPath.Dispose();
graphic.Dispose();


// turn the Image to ImageSource
MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Png);

img.Source =  (ImageSource)(new ImageSourceConverter()).ConvertFrom(memoryStream);

// Add the image to the window content
this.Content = img;

现在这个在windows 10下可以完美显示,如此:

In the windows 10 operating system

但是,如果操作系统低于Windows 10,例如:Windows XPWindows 7Windows 8.X,则会像这样显示:

enter image description here

并且不仅仅是数字,如果我输入其他字符,例如:"&&"

"سىناق&&سىناق"

低版本的操作系统仍然会将其显示为:

"سىناق سىناق"

似乎操作系统自动删除中间的字符。

我知道我使用的字体不包括我输入的数字或字符,但Windows 10可以正确显示它。

这是字体:下载字体文件

因为字符不仅在特定位置显示,所以我无法将字符串拆分并逐个呈现

所以我真的想知道为什么,也希望一些人能给我建议,在低于Windows 10的操作系统中如何解决这个问题。谢谢。


很多方法的网络库只是调用Windows DLL的包装器。因此,如果存在操作差异,则会出现与Windows DLL、PC上的区域设置不同或已安装字体不同的问题。您的PC是否安装了所有Windows更新?使用了哪些字体?如果所选字体不可用,则Windows将自动选择最接近的字体。可能这个错误是由于使用了不同的字体。找出在好和坏结果中使用的字体名称。 - jdweng
@jdweng,感谢您的回复。我已经在我的问题中放置了字体文件地址,并确保在每个操作系统中安装了所有更新。我还确认了文化设置相同,操作系统干净,没有其他字体。 - qakmak
@jdweng,正如我之前在问题中所说的那样,这个数字和一些字符“不在字体中”,但我只想让它在低版本操作系统中工作,就像Windows 10一样。 - qakmak
我无法复制那个问题!在Win 10和Win 7上都显示相同的字符串!!http://prntscr.com/m11xjj - SamTh3D3v
当字体没有所请求的代码点(也称字符)的字形时,系统必须找到另一个字体来显示它。因此,您基本上是观察这种行为。它的工作原理非常复杂,不仅取决于Windows版本,还取决于您运行的PC和使用的技术(WPF vs Winforms,本机GDI等)。 在这种情况下,Windows 10比其前身做得更好。这并不意味着您不能通过较低版本获得相同的结果。如果您真的想吓自己一下:https://msdn.microsoft.com/en-us/globalization/mt791278 - Simon Mourier
显示剩余12条评论
2个回答

0

-1

但是在Windows 8.1和Windows Server 2012中呢? - qakmak
还有XP怎么样? - qakmak
嗨,这是一个Microsoft已知的问题。如果他们只为Windows 7、Vista和一些服务器解决了这个问题,那我也无能为力;)。 - Asaf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接