-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
Milestone
Description
Currently System.Drawing.Common version "4.5.0-preview1-26216-02" doesn't support RTL on operating systems other than Windows. Here is a sample to demonstrate this issue:
SystemDrawingCommonRtlTest.csproj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="4.5.0-preview1-26216-02" />
</ItemGroup>
</Project>
Program.cs file
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Runtime.InteropServices;
namespace SystemDrawingCommonRtlTest
{
class Program
{
static void Main(string[] args)
{
var text = "آزمايش"; // = Test
var tahomaFont = new Font("Tahoma", 40, FontStyle.Regular, GraphicsUnit.Pixel);
var (height, width) = getImageSize(text, tahomaFont);
using (var pic = new Bitmap(width, height))
{
using (var graphics = Graphics.FromImage(pic))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.High;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
using (var font = tahomaFont)
{
using (var format = new StringFormat())
{
format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
graphics.DrawString(
text,
font,
new SolidBrush(Color.Black),
new Rectangle(0, 0, width, height),
format);
pic.Save($"rtl-text-{RuntimeInformation.OSDescription}.png", ImageFormat.Png);
}
}
}
}
}
private static (int Height, int Width) getImageSize(string text, Font font)
{
SizeF size;
using (var bmp = new Bitmap(1, 1))
{
using (var g = Graphics.FromImage(bmp))
{
size = g.MeasureString(text, font);
}
}
const int margin = 8;
return ((int)size.Height + margin, (int)size.Width + margin);
}
}
}fahadabdulaziz, shaahink, SirwanAfifi, vmtco, shakeri and 4 more

