Runtime: System.Drawing on Linux - text bounds are clipped

Created on 16 Nov 2018  路  3Comments  路  Source: dotnet/runtime

I need to draw multiline text on the bitmap.
if graphics has Pixel unit it works fine.
but i need to prepare bitmap for the exporting/printing, so i need to use the "Document" units.
unfortunately the text is cropped.

```C#
static void Main() {
const string outputFile = "output.png";
const float PxDpi = 96f;
const float DocumentDpi = 300f;
using(var bitmap = new Bitmap(300, 300))
using(var graphics = Graphics.FromImage(bitmap))
using(var font = new Font("DejaVu Sans", 18f))
using(var stringFormat = new StringFormat()) {
bitmap.SetResolution(PxDpi, PxDpi);
graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height);
graphics.PageUnit = GraphicsUnit.Document;
var pixelBounds = new RectangleF(50, 50, 150, 250);
var bounds = new RectangleF(
Convert(pixelBounds.X, PxDpi, DocumentDpi),
Convert(pixelBounds.Y, PxDpi, DocumentDpi),
Convert(pixelBounds.Width, PxDpi, DocumentDpi),
Convert(pixelBounds.Height, PxDpi, DocumentDpi));
graphics.DrawString("HELLO WORLD", font, Brushes.Black, bounds, stringFormat);
bitmap.Save(outputFile, ImageFormat.Png);
}
}

static float Convert(float value, float fromDpi, float toDpi) {
double scale = (double)toDpi / fromDpi;
double result = value * scale;
return (float)result;
}


on the windows platform I see the

Hello
World

on the png.
But on the linux I see the cropped text

H
W
```
on the output png.

The good side is a location of text start is right.
but the bound's size is too small.

area-System.Drawing bug

Most helpful comment

@safern, @maryamariyan as this seems to be a bug I'll mark it as 3.0.

All 3 comments

@safern, @maryamariyan as this seems to be a bug I'll mark it as 3.0.

@safern my suggestion here is move to Future as there's only one report. Also it is likely in libgdiplus not us?

Yes this is likely libgdiplus and not us. I'll move it over to libgdiplus and close this one to be investigated on their side. We just call GdipDrawString which is native code in order to produce the bitmap.

I created: https://github.com/mono/libgdiplus/issues/554

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sahithreddyk picture sahithreddyk  路  3Comments

jzabroski picture jzabroski  路  3Comments

aggieben picture aggieben  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments

nalywa picture nalywa  路  3Comments