在Visual Studio C#中的PictureBox中填充函数?

问题描述 投票:0回答:1

是否有功能以某种颜色自动填充巴西的这些状态之一,但没有它们的相邻状态?因此,例如,在c#Visual Studio中,我的图片框的坐标为150x和200y。我希望从这一点开始,围绕此点的所有灰色都应变为蓝色,但应在白色边框处停止。是否有任何功能可以执行,而无需为每个状态声明特定的多边形?感谢您的所有答案!

Brazil States

c# image visual-studio picturebox fill
1个回答
0
投票

这里我有一个想法,您可以使用recursion遍历像素并修改其颜色。

Bitmap bmp;
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    int x = e.X;
    int y = e.Y;

    bmp = (Bitmap)pictureBox1.Image;
    ModifyMap(x, y);
    pictureBox1.Image = bmp;
}

// Recursion
private void ModifyMap(int x, int y)
{
    // the color info of gray part "Color [A=255, R=153, G=153, B=153]"
    if (bmp.GetPixel(x, y).ToString() == "Color [A=255, R=153, G=153, B=153]")
    {
        bmp.SetPixel(x, y, Color.Blue);
        ModifyMap(x + 1, y);
        ModifyMap(x - 1, y);
        ModifyMap(x, y + 1);
        ModifyMap(x, y - 1);
    }
}

测试结果,

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.