BitmapImage (BitmapSource) のピクセルにアクセスし書き換える方法
例として、コントラストを変える処理を紹介する。
BitmapSource ApplyContrast(BitmapSource image, double contrast) { if (contrast == 0) return image; var bitmap = new FormatConvertedBitmap(image, PixelFormats.Gray8, null, 0); int width = bitmap.PixelWidth; int height = bitmap.PixelHeight; byte[] pixcels = new byte[width * height]; int stride = (width * bitmap.Format.BitsPerPixel + 7) / 8; bitmap.CopyPixels(pixcels, stride, 0); var offset = 256 * contrast; for (int x = 0; x < pixcels.Length; x++) { pixcels[x] = (byte)Math.Min(Math.Max(pixcels[x] * (1 + contrast) - offset, 0), 255); } return BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray8, null, pixcels, stride); }
そんなに早くないから大量の処理には向かないよ