public RgbImageAdapter(string path) { this.source = new BitmapImage(new Uri(path)); if (source.Format != PixelFormats.Bgra32) { source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0); } int width = source.PixelWidth; int height = source.PixelHeight; RgbPixel[,] result = new RgbPixel[height, width]; byte[] byteArray = new byte[height * width * 4]; int stride = width * 4; source.CopyPixels(byteArray, stride, 0); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { result[i, j] = new RgbPixel { Blue = byteArray[(i * width + j) * 4 + 0], Green = byteArray[(i * width + j) * 4 + 1], Red = byteArray[(i * width + j) * 4 + 2], Alpha = byteArray[(i * width + j) * 4 + 3], }; } } image = new RgbImage(result); }
public RgbImage(RgbPixel[,] pixels) { if (pixels == null) { throw new ArgumentNullException("pixels", "RgbImage constructor can't be invoked with null"); } this.pixels = new IPixel[pixels.GetLength(0), pixels.GetLength(1)]; Array.Copy(pixels, this.pixels, pixels.GetLength(0) * pixels.GetLength(1)); }
public static RgbImage ToRgbImage(this HslImage hslImage) { RgbPixel[,] rgbPixels = new RgbPixel[hslImage.Height, hslImage.Width]; for (int i = 0; i < hslImage.Height; i++) { for (int j = 0; j < hslImage.Width; j++) { HslPixel hslPixel = (HslPixel)hslImage.Pixels[i, j]; rgbPixels[i, j] = hslPixel.ToRgbPixel(); } } return new RgbImage(rgbPixels); }