/// <summary>
        /// Generates an image from ascii string.
        /// </summary>
        /// <param name="ascii"> String to be drawn to the empty bitmap </param>
        /// <param name="width"> Width of the newly created bitmap. </param>
        /// <param name="font"> Font to be set when drawing the ascii string. <para> * Use monospaced fonts only. </para></param>
        /// <param name="color"> Font color. </param>
        /// <returns> An image that drawn with the ascii string. </returns>
        public Bitmap ASCIIToImage(string ascii, int width, Font font, Color color)
        {
            var asciiLines = ascii.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            var dim        = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(ascii, font);
            var asciiImage = new Bitmap((int)dim.Width, (int)dim.Height);

            using (Graphics g = Graphics.FromImage(asciiImage))
            {
                for (int i = 0; i < asciiLines.Length; i++)
                {
                    g.DrawString(asciiLines[i], font, new SolidBrush(color), 0, i * font.Height);
                }
            }
            return(ImageUtils.ResizeImage(asciiImage, width));
        }
Exemple #2
0
        public static Color[] ExtractColors(Bitmap bmp, int width)
        {
            var resized      = ImageUtils.ResizeImage(bmp, width);
            var colors       = new Color[resized.Width * resized.Height];
            var lockedBitmap = new LockBitmap(resized);

            lockedBitmap.LockBits();

            for (int y = 0; y < resized.Height; y++)
            {
                for (int x = 0; x < resized.Width; x++)
                {
                    colors[x + y * resized.Width] = lockedBitmap.GetPixel(x, y);
                }
            }
            lockedBitmap.UnlockBits();
            return(colors);
        }
 /// <summary>
 ///  Processesing done: Grayscaling, Resizing, Contrast Adjustment.
 /// </summary>
 /// <param name="bmp"> Source of the bitmap to be processed and passed to the ImageToASCII method. </param>
 /// <returns> Ascii string from the processed image. </returns>
 public string GenerateASCII(Bitmap bmp, int width, int contrastThreshold)
 {
     return(ImageToASCII(ImageUtils.Grayscale(ImageUtils.ResizeImage(ImageUtils.SetContrast(bmp, contrastThreshold), width))));
 }
 /// <summary>
 ///  Processesing done: Grayscaling, Resizing.
 /// </summary>
 /// <param name="bmp"> Source of the bitmap to be processed and passed to the ImageToASCII method. </param>
 /// <returns> Ascii string from the processed image. </returns>
 public string GenerateASCII(Bitmap bmp, int width)
 {
     return(ImageToASCII(ImageUtils.Grayscale(ImageUtils.ResizeImage(bmp, width))));
 }