public static string ConvertToXPM(Bitmap bmp, string transparentColor)
{
var sb = new StringBuilder();
var colors = new List<string>();
var chars = new List<char>();
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)(index & 0xff) })[0];
chars.Add(c);
sb.Insert(colorsIndex, ",\"" + c + " c " + col + "\"");
colorsIndex += 14;
}
else c = 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;
}