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

时间:2018-12-25 11:47:44

标签: c# .net windows gdi+ gdi

我需要在WPF应用程序中generate an image from a string并显示它。这是我的代码:

// Create Image and generate string on it
ystem.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

如果我加上其他字符,例如:“ &&”

,不仅是数字
  

“سىناق&&سىناق”

较低的操作系统仍显示为:

  

“سىناقسىناق”

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

我知道其中使用的字体is not include the number or the character which I put,但是Windows 10可以正确显示它。

以下是字体:Download font file

因为字符不仅会显示在特定位置,所以I can't split the string, and render it one by one

所以我真的很想知道why,也想知道I hope some guys could give me advice to fix this problem when operating system lower than Windows 10。谢谢。

2 个答案:

答案 0 :(得分:0)

访问此链接: https://docs.microsoft.com/en-us/windows/win32/api/gdipluspath/nf-gdipluspath-graphicspath-addstring(inconstwchar_inint_inconstfontfamily_inint_inreal_inconstrect__inconststringformat)

这是Windows网站上有关GraphicsPath AddString方法以及如何使用它的方法。如果向下滚动,则会找到一个标记为需求的选项卡。仔细检查您的Windows操作系统是否满足这些要求。如果他们不这样做,那么该路径将无法工作。

答案 1 :(得分:-1)

我从过去发现了以下问题:

Generic GDI+ exception in GraphicsPath.AddString() with certain fonts and characters

在较旧版本的Windows中,我会遇到一些问题, 有指向特定KB that suppose to fix it的链接-值得尝试:)

顺便问一下,您是否尝试过使用其他字体类型或更改字体类型,只是为了检查某些内容是否会更改。

希望有帮助

Asaf