public static async Task <IMatrixProject> LoadFromImageAsync(this StorageFile file, uint maximumHeight, uint maximumWidth) { IMatrixProject returnValue = new MatrixProject() { ColorMatrix = new ColorMatrix(maximumWidth, maximumHeight) }; using (Stream imageStream = await file.OpenStreamForReadAsync()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream.AsRandomAccessStream()); // *** // *** Get the pixel mapper. // *** IPixelMapperFactory factory = new PixelMapperFactory(); IPixelMapper mapper = await factory.GetMapper(decoder.BitmapPixelFormat); PixelDataProvider data = await decoder.GetPixelDataAsync(); byte[] bytes = data.DetachPixelData(); uint width = decoder.OrientedPixelWidth; uint height = decoder.OrientedPixelHeight; if (width > maximumWidth || height > maximumHeight) { (bytes, width, height) = await decoder.ResizeImageAsync(maximumHeight, maximumWidth); } if (width <= maximumWidth && height <= maximumHeight) { uint startColumn = (uint)((maximumWidth - width) / 2.0); uint startRow = (uint)((maximumHeight - height) / 2.0); for (uint row = 0; row < height; row++) { for (uint column = 0; column < width; column++) { // *** // *** Get the color from the image data. // *** Color color = await mapper.GetPixelAsync(bytes, row, column, width, height); // *** // *** The default color item type is pixel. // *** ColorItem.ColorItemType itemType = ColorItem.ColorItemType.Pixel; if (color.A == 0 && (color.R != 0 || color.G != 0 || color.B != 0)) { // *** // *** An item with color and an alpha of 0 is a // *** sand pixel. // *** itemType = ColorItem.ColorItemType.Sand; color.A = 255; } else if (color.A == 0) { // *** // *** An item without color (black) and an alpha // *** of 0 is a sand pixel. // *** itemType = ColorItem.ColorItemType.Background; } await returnValue.ColorMatrix.SetItem(row + startRow, column + startColumn, color, itemType); } } // *** // *** Read the meta data. // *** await returnValue.RestoreImageMetaData(file); } else { throw new BadImageFormatException(); } } return(returnValue); }