0

I have a problem using GDI+ to draw a polygon which has a inner ring, I'm expecting the result to be blank because the current screen is inside the inner ring, but I got a solid fill result; If I scale down the outer ring, I can get the correct result, can anyone explain this behavior? The code is posted belowenter image description here

public static void Main()
    {
        SaveBitmap(1);
        SaveBitmap(10);
    }

    private static void SaveBitmap(double factor)
    {
        Bitmap bitmap = new Bitmap(800, 600);
        GraphicsPath path = new GraphicsPath();
        path.AddPolygon(GetOuterRing(factor));
        path.AddPolygon(GetInnerRing());

        Graphics g = Graphics.FromImage(bitmap);
        g.FillPath(new SolidBrush(Color.LightBlue), path);
        g.Dispose();

        bitmap.Save(factor + ".jpg");
        bitmap.Dispose();
    }

    private static PointF[] GetOuterRing(double factor)
    {
        return new PointF[]
        {
            new PointF((float)(-8388210.0f / factor), (float)(-4194020.5f/ factor)),
            new PointF((float)(8389000.0f/ factor), (float)(-4194020.5f/ factor)),
            new PointF((float)(8389000.0f/ factor), (float)(4194580.5f/ factor)),
            new PointF((float)(-8388210.0f/ factor),(float)(4194580.5f/ factor)),
            new PointF((float)(-8388210.0f/ factor), (float)(-4194020.5f/ factor))
        };
    }

    private static PointF[] GetInnerRing()
    {
        return new PointF[]
        {
            new PointF(-419038.4f, -209434.7f),
            new PointF(-419038.4f, 209995.7f),
            new PointF(419822.4f, 209995.7f),
            new PointF(419822.4f, -209434.7f),
            new PointF(-419038.4f, -209434.7f),
        };
    }
Edgar
  • 273
  • 1
  • 3
  • 12
  • 3
    _"because the current screen is inside the inner ring"_ -- the "current screen"? What do you mean by that? Frankly, I'm impressed you get anything even close to your intended output, as GDI+ tends to not like coordinates outside the usual -32768 to 32767 range. I don't understand the question. What are these "rings" of which you speak? What in your image above is a "ring"? Is that image the desired output, or what you actually get? Whichever it is, where is the other image? And where does "target bitmap" come in? What is that? – Peter Duniho Aug 25 '16 at 03:26
  • 2
    Looks like you've been changing these numbers until you got close, 8388210 is not exactly a random number. Set a breakpoint on the FillPath() call and look at the value of the g.ClipBounds property. That's the "maximum working range" for graphic coordinates, getting close to these bounds will cause trouble with the path getting clipped. Consider using ScaleTransform instead. – Hans Passant Aug 25 '16 at 06:47
  • @HansPassant thanks for the hint, I did check the property and found it has a limitation of width and height for 8388608.0, so you are right, it will cause trouble if I try to draw a pretty large shape. – Edgar Aug 26 '16 at 08:35

0 Answers0