dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.41k stars 984 forks source link

Graphics.DrawString result is sometimes (partially) ROT1 shifted #12446

Open mnijholt opened 1 week ago

mnijholt commented 1 week ago

Description

We create a Bitmap using a Graphics instance that is setuo like this

  Bitmap = new Bitmap(canvasWidth, canvasHeight);
  Bitmap.SetResolution(this.dpi, this.dpi);

  Graphics = Graphics.FromImage(Bitmap);
  Graphics.Clear(Color.White);

We then Draw multiple items using the Graphics instance on the image. Most of then are texts using Graphics.DrawString. Some of them contain words other numbers or combination of them.

Most of the time the result is correct but sometimes the text that is rendered on the image seems ROT1 decoded.

Image

In the image you can see the result of the following text 08 november 2024. The numbers are always correct but the letters are (as you can see) randomly correct or shifted one letter in the alphabet.

  var font = LoadFont("Roihu.otf")

  Graphics.DrawString("08 november 2024", new Font(font, 8, FontStyle.Bold), Brushes.Black, 45, 45);

  private FontFamily LoadFont(string fontName)
  {
    PrivateFontCollection collection = new PrivateFontCollection();

    using var fontStream = GetResourceFileStream(fontName);

    int fontStreamLength = (int)fontStream.Length;

    nint data = Marshal.AllocCoTaskMem(fontStreamLength);

    byte[] fontData = new byte[fontStreamLength];
    fontStream.Read(fontData, 0, fontStreamLength);

    Marshal.Copy(fontData, 0, data, fontStreamLength);

    collection.AddMemoryFont(data, fontStreamLength);

    Marshal.FreeCoTaskMem(data);

    return collection.Families[0];
  }

I first thought the issue could be related to how the font file was loaded, but when texts are messed up also other texts that are drawn on the image (using the same font) are rendered correct, so that why I assume this isn't the issue. I added this to give a complete picture.

Configuration

NET8.0 Azure App Service (windows)

Zheng-Li01 commented 6 days ago

@mnijholt, Create the project based on your description to reproduce the issue, but get the below error. Could you please modify the attached application or provide reproduce project to help repro this issue? 12446RepoApplication.zip Image

baswarmerdam commented 6 days ago

Hi @Zheng-Li01

Thank you for looking into this. I am a colleague of @mnijholt . I cannot share the font file publicly. Can I send it by email?

Zheng-Li01 commented 3 days ago

@baswarmerdam, thanks for your update. please have try to file feedback under https://learn.microsoft.com/en-us/visualstudio/ide/how-to-report-a-problem-with-visual-studio?view=vs-2022, then attached the reproduce application & font file in the feedback to help us to reproduce the issue.

baswarmerdam commented 3 days ago

Hi @Zheng-Li01

I just submitted the feedback with an attached console application to reproduce the issue. See the comments in the feedback for details. If you have any questions, please let me know.

Zheng-Li01 commented 2 days ago

@baswarmerdam, got it, thanks for your update, could you please share the feedback link on here.

baswarmerdam commented 2 days ago

@Zheng-Li01 Sure, see: https://developercommunity.visualstudio.com/t/In-reference-to-case-https:githubcom/10786734

Zheng-Li01 commented 2 days ago

As below screenshot that some letters are shifted one letter in the alphabet. Image

baswarmerdam commented 1 day ago

@Zheng-Li01 Glad you could reproduce. I am curious if this is indeed a bug in the library. Will wait for an update.

baswarmerdam commented 1 day ago

It looks like it is related to having multiple fonts. If I just use one font for everything, the issue does not seem to occur.