Weird corruption when drawing text with DrawString

Started by
0 comments, last by Tape_Worm 10 years, 2 months ago

OK, so I'm experiencing the weirdest problem. I'm drawing some text and it's showing up corrupted:

[attachment=20278:Corrupted.png]

So, as you can see, the image on the left shows the corrupted text in Windows 8.1 and on the right the actual text is being rendered correctly in Windows 7.

The corruption only happens when I have the CompositingMode set to SourceCopy and the TextRenderingHint set to SingleBitPerPixel (with and without GridFit). I require SourceCopy as my compositing mode because I need to write to the alpha channel of the bitmap instead of blending the alpha with the bitmap contents.

The code that draws this text is incredibly simple, so I am at a loss as to why this is happening.

Anyone else run into this problem? Know of a way around it?

Here's the code that I used to repro the error:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestFontAntiAlisBug
{
    public partial class Form1 : Form
    {
        private Bitmap _image;
        private Font _font;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            _font = new Font("Andalus", 48.0f, GraphicsUnit.Point);
            _image = new Bitmap(256, 256, PixelFormat.Format32bppArgb);

            using (Graphics g = Graphics.FromImage(_image))
            {
                g.Clear(Color.Black);
                g.CompositingMode = CompositingMode.SourceCopy;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;

                using (Brush brush = new SolidBrush(Color.White))
                {
                    g.DrawString("Are you corrupted?", _font, brush, new PointF(0, 0));
                }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            e.Graphics.DrawImage(_image, new Point(0, 0));
        }

        public Form1()
        {
            InitializeComponent();
        }
    }
}

Advertisement

OK, so I finally found another person that has run into the same problem (this is from 2012... I'm astounded it's still an issue):

http://social.msdn.microsoft.com/forums/windowsdesktop/en-US/7f02b531-529d-4940-a220-cde46e61e88f/windows-8-garbled-text-with-gdi-graphicsdrawstring

In the end I used the GraphicsPath method described by Gianpaolo64 (in the aforementioned link) and that solved my issue.

This topic is closed to new replies.

Advertisement