private static void computeLevels() { grayLevels = new int[AsciiArt.characterSet.Length]; Font font = AsciiArt.GetFont(); glyphs = new List <Glyph>(); for (int lvl = 0; lvl < AsciiArt.characterSet.Length; lvl++) { string s = AsciiArt.characterSet[lvl].ToString(); Bitmap bmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); g.DrawString(s, font, Brushes.Black, 0, 0); int width = (int)g.MeasureString("#", font).Width - 2; int height = (int)g.MeasureString("#", font).Height; int totalGray = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color col = bmp.GetPixel(x, y); int value = Draw.RgbToGray(col.R, col.G, col.B); totalGray += value; } } int grayLevel = (int)(255f * (float)totalGray / (float)(width * height * 255)); glyphs.Add(new Glyph(characterSet[lvl], grayLevel)); } glyphs.Sort(delegate(Glyph g1, Glyph g2) { return(g1.level.CompareTo(g2.level)); }); levelsComputed = true; }
private void btnConvert_Click(object sender, EventArgs e) { int w = 100, h = 100; int.TryParse(txtWidth.Text, out w); int.TryParse(txtHeight.Text, out h); string text = AsciiArt.Process(inputImage, w, h, textParam.Text); Font fnt = AsciiArt.GetFont(); Output dlgOut = new Output(); dlgOut.WndText = text; dlgOut.Fnt = fnt; dlgOut.ShowDialog(); }
/// <summary> /// Converts the given input bitmap into an ASCCI art string. /// </summary> /// <param name="src">Source image.</param> /// <param name="width">Required output width in characters.</param> /// <param name="height">Required output height in characters.</param> /// <param name="param">Textual parameter.</param> /// <returns>String (height x width ASCII table)</returns> public static string Process(Bitmap src, int width, int height, string param) { // !!!{{ TODO: replace this with your own bitmap -> ASCII conversion code if (src == null || width <= 0 || height <= 0) { return(""); } float widthBmp = src.Width; float heightBmp = src.Height; if (param.Length > 1) { characterSet = param.ToCharArray(); } else { characterSet = defaultCharacterSet; } //if (! levelsComputed) AsciiArt.computeLevels(); // preprocess Rectangle rectangle = new Rectangle(0, 0, src.Width, src.Height); System.Drawing.Imaging.BitmapData bitmapDataIn = src.LockBits(rectangle , System.Drawing.Imaging.ImageLockMode.ReadOnly , src.PixelFormat); IntPtr ip = bitmapDataIn.Scan0; Bitmap img = Preprocessing.Preprocess(ip, (int)widthBmp, (int)heightBmp); src.UnlockBits(bitmapDataIn); StringBuilder sb = new StringBuilder(); for (int y = 0; y < height; y++) { float fYBmp = y * heightBmp / height; for (int x = 0; x < width; x++) { float fXBmp = x * widthBmp / width; Color c = img.GetPixel((int)fXBmp, (int)fYBmp); int luma = c.R; // all channels are the same, img is in grayscale // Draw.RgbToGray(c.R, c.G, c.B); // Alternative (luma): Y = 0.2126 * R + 0.7152 * G + 0.0722 * B //int luma = (54 * (int)c.R + 183 * (int)c.G + 19 * (int)c.B) >> 8; //sb.Append( (luma < 140) ? ((luma < 90)?MAX_LEVEL:MID_LEVEL) : MIN_LEVEL ); sb.Append(AsciiArt.getCharAtLevel(luma)); } sb.Append("\r\n"); } // !!!}} return(sb.ToString()); }