其他分享
首页 > 其他分享> > CodeGo.net> OpenNETCF签名控制问题

CodeGo.net> OpenNETCF签名控制问题

作者:互联网

我在OpenNETCF中使用“签名”控件.它对我需要的大多数东西都有效.

但是,我需要一种反转签名并将其重新加载的方法.

它具有调用以获取签名的“字节”(GetSignatureEx()).它返回签名的byte [].然后可以使用LoadSignatureEx()重新加载该签名.

我似乎无法弄清楚这些字节的系统.我以为它们可能是坐标,但现在看来并非如此.

如果外面有人知道一种反转签名并将其重新加载的方法,我将不胜感激.

给其他可能在乎的人的注意事项:

这些字节似乎具有以下结构(按顺序):

2 bytes to show Width  
2 bytes to show Height  
  -- This next part repeats till the end of the array  
  2 bytes to show How many points are in the next line  
    -- This next part repeats as many times as the previous line indicated  
    1 byte for the x coordinate of the point  
    1 byte for the y coordinate of the point  
    2 bytes for the width of the pen (I am not 100% sure on this one)  

完成后,我将发布最终代码.

以后注意:
经过大量工作之后,我发现使用内置的东西来翻转视图非常容易(感谢MusiGenesis).这似乎对我来说减少了出错的可能性.

万一有人想要它,这是我未完成的代码. (我很近,但是前进到下一个“行”的内容不太正确.)(编辑:我认为我更喜欢这种方式.我更新了下面的代码.它将一直有效因为Signature控件的宽度或高度不大于256.(请参阅下面的ctacke答案).

但是首先,非常感谢MusiGenesis帮助我解决了所有这些问题.您非常有帮助,非常感谢您的努力!

现在的代码:

private void InvertSignature(ref byte[] original)
{
    int currentIndex = 0;
    short width = BitConverter.ToInt16(original, 0);
    short height = BitConverter.ToInt16(original, 2);
    while (currentIndex < original.Length - 4)
    {
        // Move past the last iteration (or the width and hight for the first time through).
        currentIndex += 4;
        // Find the length of the next segment.
        short nextGroup = BitConverter.ToInt16(original, currentIndex);
        //Advance one so we get past the 2 byte group
        currentIndex += 2;
        // Find the actual index of the last set of coordinates for this segment.
        int nextNumberOfItems = ((nextGroup) * 4) + currentIndex;
        // Invert the coordinates
        for (int i = currentIndex; i < (nextNumberOfItems - 1); i += 4)
        {
            currentIndex = i;

            //Invert Horizontal
            int newHorzPoint = width - original[i] - 1;
            if (newHorzPoint <= 0)
                newHorzPoint = 0;
            else if (newHorzPoint >= width - 1)
                newHorzPoint = width - 1;
            original[i] = (byte)newHorzPoint;

            // Invert Vertical
            int newVertPoint = height - original[i + 1] - 1;
            if (newVertPoint <= 0)
                newVertPoint = 0;
            else if (newVertPoint >= height - 1)
                newVertPoint = height - 1;
            original[i + 1] = (byte)newVertPoint;
        }
    }
}

解决方法:

完全未经测试的代码高尔夫:

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;
            }
        }
    }
}

或尝试使用此版本,假设前4个字节用于存储签名的宽度和高度(每个2个字节的短整数):

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];
            }
        }
    }
}

查看:Converting OpenNetCF GetSignatureEx to Bitmap on Desktop

更新:给出了为什么需要反转签名的描述,将设备的ScreenOrientation反转180度(然后在客户签名后返回)可能会更容易.这样,您还可以使用标签来告诉客户他们正在签名的内容-否则,他们将看到一大堆颠倒的东西(除了签名控件本身).

为此,请在项目中添加对Microsoft.WindowsCE.Forms的引用,然后使用Microsoft.WindowsCE.Forms进行添加.到文件顶部.

要将屏幕反转180度:

SystemSettings.ScreenOrientation = ScreenOrientation.Angle180;

要恢复正常:

SystemSettings.ScreenOrientation = ScreenOrientation.Angle0;

如果您在仿真器中运行此程序,则屏幕通常仍会竖直显示,但皮肤会上下颠倒翻转.

更新:基于ctacke的答案,此操作的最后一枪(这适用于任何尺寸的签名):

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;
        }
    }
}

标签:compact-framework,c,net,opennetcf
来源: https://codeday.me/bug/20191024/1918106.html