#region Using Directives using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; #endregion Using Directives namespace ScintillaNET { /// /// Converts Bitmap images to XPM data for use with ScintillaNET. /// Warning: images with more than (around) 50 colors will generate incorrect XPM /// The XpmConverter class was based on code from flashdevelop. /// internal static class XpmConverter { #region Fields /// /// The default transparent Color /// public static readonly string DefaultTransparentColor = "#FF00FF"; #endregion Fields #region Methods /// /// Converts Bitmap images to XPM data for use with ScintillaNET. /// Warning: images with more than (around) 50 colors will generate incorrect XPM. /// Uses the DefaultTransparentColor. /// /// The image to transform. public static string ConvertToXPM(Bitmap bmp) { return ConvertToXPM(bmp, DefaultTransparentColor); } /// /// Converts Bitmap images to XPM data for use with ScintillaNET. /// Warning: images with more than (around) 50 colors will generate incorrect XPM /// tColor: specified transparent color in format: "#00FF00". /// /// The image to transform. /// The overriding transparent Color public static string ConvertToXPM(Bitmap bmp, string transparentColor) { StringBuilder sb = new StringBuilder(); List colors = new List(); List chars = new List(); int width = bmp.Width; int height = bmp.Height; int index; sb.Append("/* XPM */static char * xmp_data[] = {\"").Append(width).Append(" ").Append(height).Append(" ? 1\""); int colorsIndex = sb.Length; string col; char c; for (int y = 0; y < height; y++) { sb.Append(",\""); for (int x = 0; x < width; x++) { col = ColorTranslator.ToHtml(bmp.GetPixel(x, y)); index = colors.IndexOf(col); if (index < 0) { index = colors.Count + 65; colors.Add(col); if (index > 90) index += 6; c = Encoding.ASCII.GetChars(new byte[] { (byte)(index & 0xff) })[0]; chars.Add(c); sb.Insert(colorsIndex, ",\"" + c + " c " + col + "\""); colorsIndex += 14; } else c = (char)chars[index]; sb.Append(c); } sb.Append("\""); } sb.Append("};"); string result = sb.ToString(); int p = result.IndexOf("?"); string finalColor = result.Substring(0, p) + colors.Count + result.Substring(p + 1).Replace(transparentColor.ToUpper(), "None"); return finalColor; } /// /// Cicles an image list object to convert contained images into xpm /// at the same time we add converted images into an arraylist that lets us to retrieve images later. /// Uses the DefaultTransparentColor. /// /// The image list to transform. public static List ConvertToXPM(ImageList ImageList) { return ConvertToXPM(ImageList, DefaultTransparentColor); } /// /// Cicles an image list object to convert contained images into xpm /// at the same time we add converted images into an arraylist that lets us to retrieve images later /// /// The image list to transform. /// The overriding transparent Color public static List ConvertToXPM(ImageList imageList, string transparentColor) { List xpmImages = new List(); foreach (Image image in imageList.Images) { if (image is Bitmap) { xpmImages.Add(ConvertToXPM(image as Bitmap, transparentColor)); } } return xpmImages; } #endregion Methods } }