mono / libgdiplus

C-based implementation of the GDI+ API
http://www.mono-project.com/
MIT License
334 stars 171 forks source link

Generated image is defferent than generated on Windows #630

Closed MDzyga closed 4 years ago

MDzyga commented 4 years ago

Hi, I migrated an application with .NET Core 3.1 from App Service (OS Windows) to docker container (OS linux) in Azure. After it I noticed my application generate other image (QR Code) than earlier. I didn't change anything in my code, so I think a problem is with your library (System.Drawing.Common is not avalaible on linux).

Windows: windows

Linux: linux

I used TT Norms Heavy font family (file with extension ttf).

This is my code in c#

const int _emSize = 120;

            using var qrCodeImage = await GeneratePlainQrCode(model);

            var footerTextHeight = 67;
            var headerTextHeight = 110;
            var leftMargin = 0;
            var textMargin = 0;
            var qrCodeMargin = 10;

            var sizeX = qrCodeImage.Width;
            var sizeY = qrCodeImage.Height + headerTextHeight + footerTextHeight + qrCodeMargin;
            var footerPositionY = headerTextHeight + qrCodeImage.Height + qrCodeMargin;
            var textPositionX = leftMargin + textMargin;
            var bitmap = new Bitmap(sizeX + 2 * leftMargin, sizeY);

            using var graphics = Graphics.FromImage(bitmap);
            using var brush = new SolidBrush(LabelsHelper.DarkBlueColor);
            using var headerStringFormat = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center };
            using var footerStringFormat = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center };
            using var fontCollection = new PrivateFontCollection();

            Rectangle headerRectangle;
            Rectangle mainFooterRectangle;

headerRectangle = new Rectangle(0, 0, bitmap.Size.Width, headerTextHeight);
                graphics.FillRectangle(brush, headerRectangle);
                mainFooterRectangle = new Rectangle(textPositionX, footerPositionY, sizeX, footerTextHeight);

            graphics.FillRectangle(Brushes.Transparent, mainFooterRectangle);

            fontCollection.AddFontFile(LabelsHelper.TTnormsHeavyFontFilePath);

            graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

            using var font = new Font(fontCollection.Families[0], _emSize, FontStyle.Regular, GraphicsUnit.Pixel);
            using var headerFont = LabelsHelper.FindFont(graphics, model.HeaderText, headerRectangle.Size, font);
            using var mainFooterFont = LabelsHelper.FindFont(graphics, model.MainFooterText, mainFooterRectangle.Size, font);

            LabelsHelper.DrawHeaders(graphics, headerRectangle, headerFont, headerStringFormat, Brushes.White, model);
            LabelsHelper.DrawQrCode(graphics, qrCodeImage, leftMargin, headerTextHeight + qrCodeMargin);
            LabelsHelper.DrawFooters(graphics, model, mainFooterRectangle, mainFooterFont, footerStringFormat, brush);

            return bitmap;
filipnavara commented 4 years ago

Aside from the issue with font the text is also different (char -> charr). Are you sure the font files are deployed correctly to the right path (case sensitive, correct slashes, etc.)?

MDzyga commented 4 years ago

yes, in other case I could get an exception because of it

MDzyga commented 4 years ago

Text is different because I have 2 environments and compere them

filipnavara commented 4 years ago

Can you confirm the version of libgdiplus installed in the Docker environment?

MDzyga commented 4 years ago

Yes, I will do it tomorrow morning. I noticed that all imported fonts have the same name 'TT Norms' without Bold, Extra Heavy etc. Maybe this is a problem?

MDzyga commented 4 years ago

this is my command which install libgdiplus in container

RUN apt-get update \
    && apt-get install -y --no-install-recommends libgdiplus libc6-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

locally I have installed 4.2-2 version

filipnavara commented 4 years ago

That's really old one. It had a bug with loading custom fonts.

MDzyga commented 4 years ago

could you send me proper command to install the latest? When I execute apt-get update && apt-get install -y libgdiplus, I get a messsage:

libgdiplus is already the newest version (4.2-2)

OS: Debian 10

filipnavara commented 4 years ago

The newer package is available only on Debian sid or through Mono package repositories. I suppose you would need to follow the instructions at https://www.mono-project.com/download/stable/#download-lin-debian.

MDzyga commented 4 years ago

thanks, it fixed my issue :)