Search code examples
compact-frameworkgdi+antialiasingcleartype

How to use ClearType with double buffering on Compact Framework?


When I draw a string into a buffer, the resulting output is not anti-aliased the way I'd expect. This code illustrates the problem... just put this in a standard smart device project's Form1.cs:

protected override void OnPaint(PaintEventArgs e)
{
  Bitmap buffer = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);
  using(Graphics g = Graphics.FromImage(buffer))
  {
    g.Clear(Color.White);
    g.DrawString("Hello, World", Font, new SolidBrush(Color.Black), 5, 5);
  }
  e.Graphics.DrawImage(buffer, 0, 0);
}

On the other hand, if I just draw the string into the Graphics object that was passed in with the PaintEventArgs, it renders in ClearType just as I'd expect.

I figure I've got to create my Graphics buffer in a way that makes it use font smoothing, but I don't see a way to do that.


Solution

  • Turns out to have been a simple problem. By removing the PixelFormat.Format32bppRgb it worked fine. Looks like you need to make sure your buffers have the same pixel formats...