I would assume you want to apply a convolution filter.
To start with you probably want to use a Bitmap Rather than an image, i.e. new Bitmap(Path)
.
The trivial method would be to loop over each pixel, and then loop over each value in the filter and multiply/accumulate with the corresponding pixel values. Using GetPixel to get the pixel values. Note that you need to handle the borders of the image somehow, for example by skipping them. GetPixel
is notoriously slow, but I would recommend using it to ensure you have the code nailed down before optimizing.
Something like this (Untested):
var bmp = new Bitmap(@"test.bmp");
var filter = new float[3, 3];
var result = new Bitmap(bmp.Width - 2, bmp.Height - 2);
for(int y = 1; y < bmp.Height-1; y++)
{
for (int x = 1; x < bmp.Width - 1; x++)
{
float r = 0;
r += bmp.GetPixel(x-1, y-1).R * filter[0, 0];
r += bmp.GetPixel(x , y-1).R * filter[1, 0];
r += bmp.GetPixel(x+1, y-1).R * filter[2, 0];
r += bmp.GetPixel(x-1, y ).R * filter[0, 1];
r += bmp.GetPixel(x , y ).R * filter[1, 1];
r += bmp.GetPixel(x+1, y ).R * filter[2, 1];
r += bmp.GetPixel(x-1, y+1).R * filter[0, 2];
r += bmp.GetPixel(x , y+1).R * filter[1, 2];
r += bmp.GetPixel(x+1, y+1).R * filter[2, 2];
// Repeat for G & B channels
result.SetPixel(x-1, y-1, Color.FromArgb((int)r, 0, 0));
}
}
A more comprehensive guide can be found here.