4. 膨脹處理程式
顏色不能直接 ==,要 ToArgb()
namespace Q4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
pictureBox1.Image = Bitmap.FromFile(openFileDialog1.FileName);
}
private void button2_Click(object sender, EventArgs e)
{
var bm = (Bitmap)pictureBox1.Image;
Bitmap nbm = (Bitmap)bm.Clone();
for (int x = 0; x < bm.Width; x++)
for (int y = 0; y < bm.Height; y++)
{
processPixel(bm, nbm, x, y);
}
pictureBox1.Image = nbm;
}
void processPixel(Bitmap bm, Bitmap nbm, int x, int y)
{
var c = bm.GetPixel(x, y);
if (c.ToArgb() == Color.White.ToArgb()) return;
setPixel(nbm, c, x + 1, y);
setPixel(nbm, c, x, y + 1);
setPixel(nbm, c, x, y - 1);
setPixel(nbm, c, x - 1, y);
setPixel(nbm, c, x + 1, y + 1);
setPixel(nbm, c, x - 1, y - 1);
setPixel(nbm, c, x + 1, y - 1);
setPixel(nbm, c, x - 1, y + 1);
}
void setPixel(Bitmap bm, Color source, int x, int y)
{
if (x < 0 || x >= bm.Width) return;
if (y < 0 || y >= bm.Height) return;
var c = bm.GetPixel(x, y);
if (c.ToArgb()!=Color.White.ToArgb()) return;
bm.SetPixel(x, y, source);
}
}
}
Last updated