1: /// <summary>
2: /// adds noise to the image
3: /// </summary>
4: /// <param name="OriginalImage">Image to manipulate</param>
5: /// <param name="Amount">Amount of noise to add</param>
6: /// <returns>A bitmap image</returns>
7: public static Bitmap AddNoise(Bitmap OriginalImage, int Amount)
8: {
9: Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
10: BitmapData NewData = Image.LockImage(NewBitmap);
11: BitmapData OldData = Image.LockImage(OriginalImage);
12: int NewPixelSize = Image.GetPixelSize(NewData);
13: int OldPixelSize = Image.GetPixelSize(OldData);
14: Random.Random TempRandom = new Random.Random();
15: for (int x = 0; x < NewBitmap.Width; ++x)
16: {
17: for (int y = 0; y < NewBitmap.Height; ++y)
18: {
19: Color CurrentPixel = Image.GetPixel(OldData, x, y, OldPixelSize);
20: int R = CurrentPixel.R + TempRandom.Next(-Amount, Amount + 1);
21: int G = CurrentPixel.G + TempRandom.Next(-Amount, Amount + 1);
22: int B = CurrentPixel.B + TempRandom.Next(-Amount, Amount + 1);
23: R = R > 255 ? 255 : R;
24: R = R < 0 ? 0 : R;
25: G = G > 255 ? 255 : G;
26: G = G < 0 ? 0 : G;
27: B = B > 255 ? 255 : B;
28: B = B < 0 ? 0 : B;
29: Color TempValue = Color.FromArgb(R, G, B);
30: Image.SetPixel(NewData, x, y, TempValue, NewPixelSize);
31: }
32: }
33: Image.UnlockImage(NewBitmap, NewData);
34: Image.UnlockImage(OriginalImage, OldData);
35: return NewBitmap;
36: }