1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39:
| private void button1_Click(object sender, EventArgs e) { Graphics graphics = Graphics.FromHwnd(Handle); graphics.DrawEllipse(Pens.Gray, new Rectangle(50, 50, 200, 200)); graphics.FloodFill(new Point(10, 10), Color.Blue, FloodFillType.Surface); }
public enum FloodFillType { Border, Surface }
public static class Extensions { [DllImport("gdi32.dll")] private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj); [DllImport("gdi32.dll")] private static extern IntPtr CreateSolidBrush(int crColor); [DllImport("gdi32.dll")] private static extern bool ExtFloodFill(IntPtr hdc, int nXStart, int nYStart, int crColor, uint fuFillType); [DllImport("gdi32.dll")] private static extern bool DeleteObject(IntPtr hObject); [DllImport("gdi32.dll")] public static extern int GetPixel(IntPtr hdc, int x, int y);
public static void FloodFill(this Graphics graphics, Point point, Color color, FloodFillType type) { IntPtr dc = graphics.GetHdc(); IntPtr brush = CreateSolidBrush(ColorTranslator.ToWin32(color)); IntPtr previousBrush = SelectObject(dc, brush); ExtFloodFill(dc, point.X, point.Y, GetPixel(dc, point.X, point.Y), type == FloodFillType.Border ? (uint)0 : 1); SelectObject(dc, previousBrush); DeleteObject(brush); graphics.ReleaseHdc(dc); } } |