public Image CreateUpperRightToLowerLeftTriangleBlockMosaic(int blockSize, Bitmap image) { if (blockSize <= 0) { return null; } var newBitmap = new Bitmap(image); Graphics graphics = Graphics.FromImage(newBitmap); for (int column = 0; column < image.Height; column += blockSize) { for (int row = 0; row < image.Width; row += blockSize) { var averagingArea = new Rectangle(row, column, blockSize, blockSize); Color averageTopColor = PixelFactory.GetAverageColorOfTopRightTriangle(averagingArea, image); var averageTopBrush = new SolidBrush(averageTopColor); Point[] topPoints = this.getTopRightTrianglePoints(blockSize, row, column); graphics.FillPolygon(averageTopBrush, topPoints); Color averageBottomColor = PixelFactory.GetAverageColorOfBottomLeftTriangle(averagingArea, image); var averageBottomBrush = new SolidBrush(averageBottomColor); Point[] botPoints = this.getBottomLeftTrianglePoints(blockSize, row, column); graphics.FillPolygon(averageBottomBrush, botPoints); } } return newBitmap; }
/// <summary> /// Creates the picture mosaic. /// </summary> /// <param name="blockSize">Size of the block.</param> /// <param name="bitmap">The image.</param> /// <param name="palette">The palette.</param> /// <returns></returns> public Image CreatePictureMosaic(int blockSize, Bitmap bitmap, ImageList.ImageCollection palette) { Graphics g = Graphics.FromImage(bitmap); List<Image> images = this.copyPalette(palette); List<Color> colors = this.averagePaletteColors(images); for (int y = 0; y < bitmap.Height; y += blockSize) { for (int x = 0; x < bitmap.Width; x += blockSize) { var dstRect = new Rectangle(x, y, blockSize, blockSize); Color averageColor = PixelFactory.GetAverageColorOfSquareBlock(dstRect, bitmap); List<Image> closeMatches = this.getBestMatchPaletteImage(averageColor, images, colors); Image randomMatch = this.getRandomBestMatch(closeMatches); this.previousImage = randomMatch; var srcRect = new Rectangle(0, 0, randomMatch.Width, randomMatch.Height); g.DrawImage(randomMatch, dstRect, srcRect, GraphicsUnit.Pixel); } } g.Dispose(); return bitmap; }
private List<Color> averagePaletteColors(IEnumerable<Image> images) { var averageColors = new List<Color>(); foreach (Image image in images) { var srcRect = new Rectangle(0, 0, image.Width, image.Height); Color currentAverage = PixelFactory.GetAverageColorOfSquareBlock(srcRect, (Bitmap) image); averageColors.Add(currentAverage); } return averageColors; }
/// <summary> /// Creates the square block mosaic. /// </summary> /// <param name="blockSize">Size of the block.</param> /// <param name="image">The image.</param> /// <returns></returns> public Image CreateSquareBlockMosaic(int blockSize, Bitmap image) { if (blockSize <= 0) { return null; } var newBitmap = new Bitmap(image); Graphics graphics = Graphics.FromImage(newBitmap); for (int column = 0; column < image.Height; column += blockSize) { for (int row = 0; row < image.Width; row += blockSize) { var averagingArea = new Rectangle(row, column, blockSize, blockSize); Color averageColor = PixelFactory.GetAverageColorOfSquareBlock(averagingArea, image); var averageBrush = new SolidBrush(averageColor); graphics.FillRectangle(averageBrush, row, column, blockSize, blockSize); } } return newBitmap; }