SignatureXtra String Conversion

SignatureXtra String Conversion


public static Bitmap StringToBitmap(string s)
{
    byte[] b = StrToByteArray(s);
    Bitmap bmp = CreateBitmap(b);
    return bmp;
}

public static byte[] StrToByteArray(string s)
{
    try
    {
        s = s.Replace(" ", string.Empty);
        byte[] buffer = new byte[s.Length / 2];
        for (int i = 0; i < s.Length; i += 2)
            buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
        return buffer;
    }
    catch {
        return null;
    }
}

public static System.Drawing.Bitmap CreateBitmap(byte[] b)
{
    if (b == null)
        return null;
    if (b.Length < 4)
        return null;
    int minX = int.MaxValue;
    int minY = int.MaxValue;
    int maxX = int.MinValue;
    int maxY = int.MinValue;
    for (int i = 0; i < b.Length; i += 2)
    {
        if ((b[i] == byte.MaxValue) && (b[i + 1] == byte.MaxValue))
            continue;
        minX = ((int)b[i] < minX) ? (int)b[i] : minX;
        maxX = ((int)b[i] > maxX) ? (int)b[i] : maxX;
        minY = ((int)b[i + 1] < minY) ? (int)b[i + 1] : minY;
        maxY = ((int)b[i + 1] > maxY) ? (int)b[i + 1] : maxY;
    }
    int w = 2 + maxX - minX;
    int h = 2 + maxY - minY;
    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(w, h);
    System.Drawing.Graphics cc = System.Drawing.Graphics.FromImage(bmp);
    System.Drawing.SolidBrush brush1 = new System.Drawing.SolidBrush(System.Drawing.Color.White);
    cc.FillRectangle(brush1, 0, 0, bmp.Width, bmp.Height);
    System.Drawing.Pen p = new System.Drawing.Pen(System.Drawing.Color.Black);
    int lastX = (int)b[0];
    int lastY = (int)b[1];
    for (int i = 2; i < b.Length; i += 2)
    {
        if ((b[i] == byte.MaxValue) && (b[i + 1] == byte.MaxValue))
        {
            i += 2;
            if (i >= b.Length)
                break;
            lastX = (int)b[i];
            lastY = (int)b[i + 1];
        }
        else
        {
            cc.DrawLine(p, lastX, lastY + 1, (int)b[i], (int)b[i + 1] + 1);
            cc.DrawLine(p, lastX, lastY, (int)b[i], (int)b[i + 1]);
            lastX = (int)b[i];
            lastY = (int)b[i + 1];
        }
    }
    return bmp;
}