Exemplo n.º 1
0
        private Stream Asciirize(Stream sourceStream, FontInfo fontInfo)
        {
            using (var sourceImage = new Bitmap(sourceStream))
                using (var targetImage = new Bitmap(sourceImage.Width, sourceImage.Height))
                    using (var targetGraphics = Graphics.FromImage(targetImage))
                    {
                        var graphicsUnit = GraphicsUnit.Pixel;
                        var sourceBounds = sourceImage.GetBounds(ref graphicsUnit);

                        targetGraphics.FillRectangle(new SolidBrush(Color.Black), new Rectangle(Point.Empty, targetImage.Size));
                        for (var widthPos = 0; widthPos < sourceImage.Width; widthPos += fontInfo.Width)
                        {
                            for (var heightPos = 0; heightPos < sourceImage.Height; heightPos += fontInfo.Height)
                            {
                                var rect = new Rectangle(widthPos, heightPos, fontInfo.Width, fontInfo.Height);
                                if (!sourceBounds.Contains(rect))
                                {
                                    break;
                                }

                                using (var snippet = sourceImage.Clone(rect, sourceImage.PixelFormat))
                                {
                                    var matchingCharacter = _characterMapper.FindBestVisualMatch(fontInfo, snippet);
                                    var centerColor       = snippet.GetPixel(fontInfo.Width / 2, fontInfo.Height / 2);
                                    targetGraphics.DrawString(matchingCharacter, fontInfo.Font, new SolidBrush(centerColor), widthPos, heightPos);
                                }
                            }
                        }

                        var resultStream = new MemoryStream();
                        targetGraphics.Flush();
                        targetImage.Save(resultStream, sourceImage.RawFormat);

                        resultStream.Seek(0, SeekOrigin.Begin);
                        return(resultStream);
                    }
        }