33,008
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace picbsftest
{
public partial class Form1 : Form
{
private Bitmap bmpBack;
private bool[,] matrix;
public Form1()
{
InitializeComponent();
bmpBack = new Bitmap(pictureBox1.Image);
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Bitmap bmpnew = new Bitmap(bmpBack);
matrix = new bool[bmpBack.Width, bmpBack.Height];
dfs(e.X, e.Y, bmpnew, 0);
for (int i = 1; i < matrix.GetLength(0) - 1; i++)
{
for (int j = 1; j < matrix.GetLength(1) - 1; j++)
{
if (matrix[i, j])
{
if (!matrix[i - 1, j])
bmpnew.SetPixel(i - 1, j, Color.Red);
if (!matrix[i + 1, j])
bmpnew.SetPixel(i + 1, j, Color.Red);
if (!matrix[i, j - 1])
bmpnew.SetPixel(i, j - 1, Color.Red);
if (!matrix[i, j + 1])
bmpnew.SetPixel(i, j + 1, Color.Red);
}
}
}
pictureBox1.Image = bmpnew;
}
private void dfs(int x, int y, Bitmap bmp, int level)
{
if (x < 0 || y < 0 || x >= bmp.Width || y >= bmp.Height)
return;
if (matrix[x, y])
return;
if (bmp.GetPixel(x, y).ToArgb() == -16777216)
return;
matrix[x, y] = true;
dfs(x - 1, y, bmp, level + 1);
dfs(x + 1, y, bmp, level + 1);
dfs(x, y - 1, bmp, level + 1);
dfs(x, y + 1, bmp, level + 1);
}
}
}