Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mnijholt opened this issue Nov 7, 2024 · 9 comments
Open

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

mnijholt opened this issue Nov 7, 2024 · 9 comments
Labels
area-System.Drawing System.Drawing issues

Comments

@mnijholt
Copy link

mnijholt commented Nov 7, 2024

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
Copy link
Member

@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
Copy link

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?

@elachlan elachlan added the area-System.Drawing System.Drawing issues label Nov 9, 2024
@Zheng-Li01
Copy link
Member

@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
Copy link

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
Copy link
Member

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

@baswarmerdam
Copy link

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

@Zheng-Li01
Copy link
Member

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

@baswarmerdam
Copy link

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

@baswarmerdam
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Drawing System.Drawing issues
Projects
None yet
Development

No branches or pull requests

4 participants