

Using StringBuilder.Append(Char) is very fast because it just copies the scalar char value directly into StringBuilder's internal char buffer.Append char values directly instead of converting a Char to a String with.
Ascii art one line code#
Your code can also be modified to be more efficient with StringBuilder:.If ( avgBrightness < 51 ) sb.Append( '█' ) Įlse if( avgBrightness < 102 ) sb.Append( '▓' ) Įlse if( avgBrightness < 153 ) sb.Append( '▒' ) Įlse if( avgBrightness < 204 ) sb.Append( '░' ) Single avgBrightness = ( (Single)r + (Single)g + (Single)b ) / 3f Throw new NotSupportedException( "meh" ) Int32 bytesPerPixel = ( bitmapData.PixelFormat ) / 8 StringBuilder sb = new StringBuilder( capacity: bitmapData.Width * bitmapData.Height ) Rectangle r = new Rectangle( x: 0, y: 0, width: bitmap.Width, height: bitmap.Height ) īitmapData bitmapData = bitmap.LockBits( rect: r, flags: ImageLockMode.ReadOnly, bitmap.PixelFormat ) Using( Bitmap bitmap = (Bitmap)( imageFile.FullName ) ) Something like this: unsafe static String RenderPixelsAsAscii_LockBits( FileInfo imageFile ) You can significantly increase performance by using Bitmap.Lockbits instead of iterating over.Problem 2: Performance Use Bitmap.Lockbits: I also made a version that uses Unicode block chars, just to make sure I was decoding the image correctly: static readonly Char _chars = new I've also increased the font-size from 3 to 10.Change the your XAML to this and it will work as-expected:.The problem isn't extra line-breaks being added, but that the rendered lines are being visually compressed.using 's default FontFamily which will be Segoe UI. Your posted XAML shows you're not setting FontFamily on the TextBlock.ASCII-Art needs to be rendered with a fixed-width (aka monospaced) font.Problem 1: ASCII art not being displayed properly in WPF TextBlock: Image To Ascii Art Code void TurnImageToAscii(Bitmap image) Here you can see that the output log displays the art correctly while the textblock has line breaks, also I experimented with TextBox and RichTextBox and they gave me the same result I made a simple app in C# WPF that displays ascii art from an image, the code seems to work but the problem is that whenever I set the text of the textblock to the ascii art it seems to have strange line breaks that have nothing to do with my code
