Well im rendering the same path 2 times one time with a white brush on a black background and one with a black brush on a white background.
Now the result should be the same image but inverted right?
Well wrong the one that’s drawn on the black background is a few pixels bigger..


Diff

Code i used to draw these
PointF[] points = LoadXml<PointF[]>(@"T:\Points.xml");
byte[] types = LoadXml<byte[]>(@"T:\Types.xml");
GraphicsPath path = new GraphicsPath(points, types, FillMode.Alternate);
using (Image image = new Bitmap(45, 45))
{
using (Graphics g = Graphics.FromImage(image))
{
Render(g, Color.White, Color.Black, path);
}
image.Save(@"T:\White.bmp", ImageFormat.Bmp);
}
using (Image image = new Bitmap(45, 45))
{
using (Graphics g = Graphics.FromImage(image))
{
Render(g, Color.Black, Color.White, path);
}
image.Save(@"T:\Black.bmp", ImageFormat.Bmp);
}
private static T LoadXml<T>(string file)
{
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (Stream stream = File.OpenRead(file))
{
return (T)xmlSerializer.Deserialize(stream);
}
}
private static void Render(Graphics g, Color mainColor, Color backColor, GraphicsPath graphicsPath)
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.Clear(backColor);
using (SolidBrush mainBrush = new SolidBrush(mainColor))
{
g.FillPath(mainBrush, graphicsPath);
}
}
Is there any way to make them render the same way without modifying the path?
Atmittedly, I am just guessing, but:
It could be the result of GDI+ attempting to do gamma-correct rendering. (I didn’t check that it does).
Historically, display monitors are non-linear. So a pixel with a grayscale value of 255 is much more than twice as bright as a pixel with value 128. So, if you are trying to anti-alias a line, and want a draw a pixel that is 50% covered with white, a pixel with value 128 is too dark. It is more like 25% white. To get a 50% white pixel, you should take a value around 180 (I don’t remember exactly).
This would cause the white image to be half a pixel larger than the black one, if you compare it to the inverse.