OpenNETCF Signature control question

只愿长相守 提交于 2019-12-01 20:45:28
MusiGenesis

Completely-untested-code Golf:

public void InvertSignature(ref byte[] original, 
    bool invertHorizontal, bool invertVertical)
{
    for (int i = 0; i < original.Length; i += 2)
    {
        if ((original[i] != 0) && (original[i + 1] != 0))
        {
            if (invertHorizontal)
            {
                original[i] = 232 - original[i] - 1;
            }
            if (invertVertical)
            {
                original[i + 1] = 64 - original[i + 1] - 1;
            }
        }
    }
}

Or try this version, on the assumption that the first 4 bytes are used to store the width and height of the signature (2 byte short ints for each):

public void InvertSignature(ref byte[] original, 
    bool invertHorizontal, bool invertVertical)
{
    byte w = (byte)BitConverter.ToInt16(original, 0) - 1;
    byte h = (byte)BitConverter.ToInt16(original, 2) - 1;
    // TO DO: blow up if w or h are > 255
    for (int i = 4; i < original.Length; i += 2)
    {
        if ((original[i] != 0) && (original[i + 1] != 0))
        {
            if (invertHorizontal)
            {
                original[i] = w - original[i];
            }
            if (invertVertical)
            {
                original[i + 1] = h - original[i + 1];
            }
        }
    }
}

See: Converting OpenNetCF GetSignatureEx to Bitmap on Desktop

Update: Given your description of why you need to invert the signature, it might be easier for you to just invert the ScreenOrientation of your device by 180 degrees (and then back after the customer signs). This way you could also have labels that tell the customer what they're signing - otherwise they're going to be looking at a bunch of upside-down stuff (other than the signature control itself).

To do this, add a reference to Microsoft.WindowsCE.Forms to your project, then add using Microsoft.WindowsCE.Forms; to the top of your file.

To invert the screen by 180 degrees:

SystemSettings.ScreenOrientation = ScreenOrientation.Angle180;

To set back to normal:

SystemSettings.ScreenOrientation = ScreenOrientation.Angle0;

If you're running this in the emulator, your screen will still appear normally upright, but the skin gets flipped upside-down.

Update: one last shot at this, based on ctacke's answer (this should work for signatures with any dimensions):

public void InvertSignature(ref byte[] original, 
    bool invertHorizontal, bool invertVertical)
{
    short w = BitConverter.ToInt16(original, 0);
    short h = BitConverter.ToInt16(original, 2);
    int i = 4;
    while (i < original.Length)
    {
        if (invertHorizontal)
        {
            if (w < 256)
            {
                if (original[i] != 0)
                {
                    original[i] = (byte)w - original[i] - 1;
                }
                i++;
            }
            else
            {
                short val = BitConverter.ToInt16(original, i);
                if (val != 0)
                {
                    val = w - val - 1;
                    byte[] valbytes = BitConverter.GetBytes(val);
                    Buffer.BlockCopy(valbytes, 0, original, i, 2);
                }
                i += 2;
            }
        }
        else
        {
            i += (w < 256) ? 1 : 2;
        }
        if (invertVertical)
        {
            if (h < 256)
            {
                if (original[i] != 0)
                {
                    original[i] = (byte)h - original[i] - 1;
                }
                i++;
            }
            else
            {
                short val = BitConverter.ToInt16(original, i);
                if (val != 0)
                {
                    val = h - val - 1;
                    byte[] valbytes = BitConverter.GetBytes(val);
                    Buffer.BlockCopy(valbytes, 0, original, i, 2);
                }
                i += 2;
            }
        }
        else
        {
            i += (h < 256) ? 1 : 2;
        }
    }
}

I might be a little late, but I'm looking at the code right now and here are some points worth noting.

  • The first 2 bytes are the width.
  • The next 2 bytes are the Height.
  • The remainder of the data are X,Y coordinates, however the storage format is deceptive and it can change. If the dimension (x or y) is < 256, we use one byte to store the value. If it's greater, we use 2 bytes. This means you might see vales stored as XYXY, XXYXXY or XYYXYY.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!