FloodFill Objects Using C#

In order to floodfill drawn objects, you can make use of the following sample:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace FloodFill{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        Bitmap bm;        Graphics g;        private bool SameColor(Color c1, Color c2)        {            return ((c1.A == c2.A) && (c1.B == c2.B) && (c1.G == c2.G) && (c1.R == c2.R));        }        private void tobien1(Bitmap bm, Point p, Color Color, Color LineColor)        {            Stack S = new Stack();            S.Push(p);            while (S.Count != 0)            {                p = S.Pop();                Color CurrentColor =  bm.GetPixel(p.X, p.Y);                if (!SameColor(CurrentColor, Color) && !SameColor(CurrentColor, LineColor))                {                    bm.SetPixel(p.X, p.Y, Color);                    S.Push(new Point(p.X - 1, p.Y));                    S.Push(new Point(p.X + 1, p.Y));                    S.Push(new Point(p.X, p.Y - 1));                    S.Push(new Point(p.X, p.Y + 1));                }            }        }        private void Form1_Load(object sender, EventArgs e)        {            g = this.CreateGraphics();        }        private void DrawRectangle(Bitmap bm, Point p, int width, int height, Color c)        {            Graphics g = Graphics.FromImage(bm);            g.DrawRectangle(new Pen(c), p.X, p.Y, width, height);        }        private void Form1_Paint(object sender, PaintEventArgs e)        {            bm = new Bitmap(ClientSize.Width, ClientSize.Height);            DrawRectangle(bm, new Point(100,100), 200, 50, Color.Blue);            g.DrawImage(bm, new Point(0, 0));        }        private void button1_Click(object sender, EventArgs e)        {            tobien1(bm, new Point(120,120), Color.Violet, Color.Blue);            g.DrawImage(bm, new Point(0, 0));        }    }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may