Jump to content

  • Log In with Google      Sign In   
  • Create Account


Weird corruption when drawing text with DrawString


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Tape_Worm   Crossbones+   -  Reputation: 1662

Like
0Likes
Like

Posted 07 March 2014 - 10:18 AM

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

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();
        }
    }
}



Sponsor:

#2 Tape_Worm   Crossbones+   -  Reputation: 1662

Like
0Likes
Like

Posted 08 March 2014 - 06:59 PM

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.

 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS