任意図形の塗りつぶし
ピクチャーボックス内の図形を、色と始点を指定して塗りつぶす関数"paint"です。
Graphics g;
Bitmap bmp;
private void Form1_Load(object sender, EventArgs e)
{
bmp = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
pictureBox1.Image = bmp;
g = Graphics.FromImage(pictureBox1.Image);
}
void paint(Color CL,int mx0,int my0)
{
int mx = mx0;
int my = my0;
int umax, vmax;
umax = pictureBox1.Width;
vmax = pictureBox1.Height;
int[,] DD = new int[umax, vmax];
Color cx = bmp.GetPixel(mx, my);
//始点を含む縦の範囲を知る
aa:;
for (int j = my; j < vmax; j++)
{
if (bmp.GetPixel(mx, j) == cx) { DD[mx, j] = 1; } else { goto xx; }
}
xx:;
for (int j = my; j >= 0; j--)
{
if (bmp.GetPixel(mx, j) == cx) { DD[mx, j] = 1; } else { goto yy; }
}
yy:;
//x方向の正の向きを調べる
for (int i = mx + 1; i < umax; i++)
{
int nn = 0;
for (int j = 0; j < vmax; j++)
{
if (DD[i - 1, j] == 1)
{
for (int j1 = j; j1 < vmax; j1++)
{
if (bmp.GetPixel(i, j1) == cx) { DD[i, j1] = 1; nn++; } else { goto xx1; }
}
xx1:;
for (int j1 = j; j1 >= 0; j1--)
{
if (bmp.GetPixel(i, j1) == cx) { DD[i, j1] = 1; nn++; } else { goto yy1; }
}
yy1:;
}
}
if (nn == 0) { goto zz; }
}
zz:;
//x方向の負の向きを調べる
for (int i = mx - 1; i >= 0; i--)
{
int nn = 0;
for (int j = 0; j < vmax; j++)
{
if (DD[i + 1, j] == 1)
{
for (int j1 = j; j1 < vmax; j1++)
{
if (bmp.GetPixel(i, j1) == cx) { DD[i, j1] = 1; nn++; } else { goto xx1; }
}
xx1:;
for (int j1 = j; j1 >= 0; j1--)
{
if (bmp.GetPixel(i, j1) == cx) { DD[i, j1] = 1; nn++; } else { goto yy1; }
}
yy1:;
}
}
if (nn == 0) { goto ww; }
}
ww:;
//塗り残しがないかを調べる
for (int ii = 1; ii < umax - 1; ii++)
{
for (int jj = 1; jj < vmax - 1; jj++)
{
if (DD[ii, jj] == 1)
{
for (int iii = -1; iii <= 1; iii = iii + 2)
{
if (DD[ii + iii, jj] == 0 & bmp.GetPixel(ii + iii, jj) == cx)
{ mx = ii + iii; my = jj; goto aa; }
}
for (int jjj = -1; jjj <= 1; jjj = jjj + 2)
{
if (DD[ii, jj + jjj] == 0 & bmp.GetPixel(ii, jj + jjj) == cx)
{ mx = ii; my = jj + jjj; goto aa; }
}
}
}
}
//塗る
for (int ii = 0; ii < umax; ii++)
{
for (int jj = 0; jj < vmax; jj++)
{
if (DD[ii, jj] == 1) { bmp.SetPixel(ii, jj, CL); }
}
}
pictureBox1.Refresh();
}
Graphics g;
Bitmap bmp;
private void Form1_Load(object sender, EventArgs e)
{
bmp = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
pictureBox1.Image = bmp;
g = Graphics.FromImage(pictureBox1.Image);
}
void paint(Color CL,int mx0,int my0)
{
int mx = mx0;
int my = my0;
int umax, vmax;
umax = pictureBox1.Width;
vmax = pictureBox1.Height;
int[,] DD = new int[umax, vmax];
Color cx = bmp.GetPixel(mx, my);
//始点を含む縦の範囲を知る
aa:;
for (int j = my; j < vmax; j++)
{
if (bmp.GetPixel(mx, j) == cx) { DD[mx, j] = 1; } else { goto xx; }
}
xx:;
for (int j = my; j >= 0; j--)
{
if (bmp.GetPixel(mx, j) == cx) { DD[mx, j] = 1; } else { goto yy; }
}
yy:;
//x方向の正の向きを調べる
for (int i = mx + 1; i < umax; i++)
{
int nn = 0;
for (int j = 0; j < vmax; j++)
{
if (DD[i - 1, j] == 1)
{
for (int j1 = j; j1 < vmax; j1++)
{
if (bmp.GetPixel(i, j1) == cx) { DD[i, j1] = 1; nn++; } else { goto xx1; }
}
xx1:;
for (int j1 = j; j1 >= 0; j1--)
{
if (bmp.GetPixel(i, j1) == cx) { DD[i, j1] = 1; nn++; } else { goto yy1; }
}
yy1:;
}
}
if (nn == 0) { goto zz; }
}
zz:;
//x方向の負の向きを調べる
for (int i = mx - 1; i >= 0; i--)
{
int nn = 0;
for (int j = 0; j < vmax; j++)
{
if (DD[i + 1, j] == 1)
{
for (int j1 = j; j1 < vmax; j1++)
{
if (bmp.GetPixel(i, j1) == cx) { DD[i, j1] = 1; nn++; } else { goto xx1; }
}
xx1:;
for (int j1 = j; j1 >= 0; j1--)
{
if (bmp.GetPixel(i, j1) == cx) { DD[i, j1] = 1; nn++; } else { goto yy1; }
}
yy1:;
}
}
if (nn == 0) { goto ww; }
}
ww:;
//塗り残しがないかを調べる
for (int ii = 1; ii < umax - 1; ii++)
{
for (int jj = 1; jj < vmax - 1; jj++)
{
if (DD[ii, jj] == 1)
{
for (int iii = -1; iii <= 1; iii = iii + 2)
{
if (DD[ii + iii, jj] == 0 & bmp.GetPixel(ii + iii, jj) == cx)
{ mx = ii + iii; my = jj; goto aa; }
}
for (int jjj = -1; jjj <= 1; jjj = jjj + 2)
{
if (DD[ii, jj + jjj] == 0 & bmp.GetPixel(ii, jj + jjj) == cx)
{ mx = ii; my = jj + jjj; goto aa; }
}
}
}
}
//塗る
for (int ii = 0; ii < umax; ii++)
{
for (int jj = 0; jj < vmax; jj++)
{
if (DD[ii, jj] == 1) { bmp.SetPixel(ii, jj, CL); }
}
}
pictureBox1.Refresh();
}