/// <summary> /// Update Value for the passed font /// </summary> /// <param name="font">Font to be used</param> public void CalculateValue(Font font) { using (Bitmap bmp = FontFunctions.DrawText(Character.ToString(), font)) { if (bmp == null) { Value = -1; } else { Width = bmp.Width; int total = 0; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < Width; x++) { total += bmp.GetPixel(x, y).R; } } Value = (int)(((float)total / (float)(Width * bmp.Height)) + 0.5); } } }
/// <summary> /// calculate the values for the current character /// </summary> /// <param name="font">font to be used</param> public void CalculateValues(Font font) { int width = 4; int height = 4; using (Bitmap bmp = FontFunctions.DrawText(Character.ToString(), font)) { using (Bitmap shrunk = new Bitmap(width, height)) { using (Graphics g = Graphics.FromImage(shrunk)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.Clear(Color.White); g.DrawImage(bmp, 0, 0, width, height); } bmp.Dispose(); int total = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { total += shrunk.GetPixel(x, y).R; } } Value = (int)(((float)total / (float)(width * height)) + 0.5); Score = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Score += Math.Max(Value, shrunk.GetPixel(x, y).R) - Math.Min(Value, shrunk.GetPixel(x, y).R); } } Score = (int)(((float)Score / (float)(width * height)) + 0.5); } } }
/// <summary> /// Save the text as an image file, will overwrite it if the file already exists /// </summary> /// <param name="text">The text to save</param> /// <param name="filename">Filename to save</param> /// <param name="font">Font to use for the text</param> /// <param name="textcolor">Color of the text</param> /// <param name="backgroundcolor">Color of the background</param> /// <param name="scale">Percentage scale of the image, 1.0-100.0</param> /// <param name="greyscale">Save the image as greyscale?</param> /// <returns>did the file save without errors?</returns> public static bool SaveTextAsImage(string text, string filename, Font font, Color textcolor, Color backgroundcolor, float scale, bool greyscale) { System.Guid format; switch (System.IO.Path.GetExtension(filename).ToLower()) { case ".png": format = ImageFormat.Png.Guid; break; case ".jpg": case ".jpeg": case ".jpe": format = ImageFormat.Jpeg.Guid; break; case ".gif": format = ImageFormat.Gif.Guid; break; case ".tif": case ".tiff": format = ImageFormat.Tiff.Guid; break; case ".bmp": case ".rle": case ".dib": default: format = ImageFormat.Bmp.Guid; break; } using (Bitmap bmpFullSize = FontFunctions.DrawText(text, font, textcolor, backgroundcolor)) { if (scale < 100f) { float fMagnification = scale / 100f; Size newSize = new Size((int)((bmpFullSize.Width * fMagnification) + 0.5), (int)((bmpFullSize.Height * fMagnification) + 0.5)); if (newSize.Width < 1) { newSize.Width = 1; } if (newSize.Height < 1) { newSize.Height = 1; } using (Bitmap bmpOutput = new Bitmap(newSize.Width, newSize.Height)) { using (ImageAttributes ia = new ImageAttributes()) { ia.SetColorMatrix(greyscale ? Matrices.Grayscale() : Matrices.Identity()); using (Graphics g = Graphics.FromImage(bmpOutput)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(bmpFullSize, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, bmpFullSize.Width, bmpFullSize.Height, GraphicsUnit.Pixel, ia); } } bmpOutput.Save(filename, new ImageFormat(format)); } } else { bmpFullSize.Save(filename, new ImageFormat(format)); } } return(true); }