我需要在我的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
下可以完美显示,如此:
但是,如果操作系统低于Windows 10,例如:Windows XP
,Windows 7
,Windows 8.X
,则会像这样显示:
并且不仅仅是数字,如果我输入其他字符,例如:"&&"
"سىناق&&سىناق"
低版本的操作系统仍然会将其显示为:
"سىناق سىناق"
似乎操作系统自动删除中间的字符。
我知道我使用的字体不包括我输入的数字或字符
,但Windows 10可以正确显示它。
这是字体:下载字体文件
因为字符不仅在特定位置显示,所以我无法将字符串拆分并逐个呈现
。
所以我真的想知道为什么
,也希望一些人能给我建议,在低于Windows 10的操作系统中如何解决这个问题。谢谢。