3
votes

I'm rendering an image into a System.Drawing.Bitmap and subsequently drawing it into a window, however I can see that the edges are being anti-aliased. How do prevent this?

Some more detail. The bitmap is created like thus:

new Bitmap (this.Width, this.Height, Imaging.PixelFormat.Format32bppArgb)

I then set pixels to either Color.Black or Color.White. I've tried using both Bitmap.SetPixel and writing bytes directly to the bitmap data using Bitmap.LockBits.

Once the bitmap is ready I draw it in my Form.OnPaint override:

            pea.Graphics.DrawImage
                ( !this.bitmap
                , this.ClientRectangle
                , new Rectangle (0, 0, this.Width, this.Height)
                , GraphicsUnit.Pixel
                )

Every pixel should either black or white however I can see that pixels at the edges are grey.

1

1 Answers

10
votes

Set the InterpolationMode property to NearestNeighbor and PixelOffsetMode to None.

pea.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
pea.Graphics.PixelOffsetMode = PixelOffsetMode.None; // or PixelOffsetMode.Half

Drawing the bitmap unscaled is best. In which case you probably want to use the ClientSize.Width and Height properties to initialize the bitmap. Odds are good that you are making the bitmap too large right now by including the form's border and caption. I can't tell from the snippet.