将InkCanvas笔划转换为字节数组并再次返回

时间:2015-06-17 15:37:00

标签: c# wpf bytearray converter inkcanvas

我正在开发一个程序,它将inkcanvas笔划转换为字节数组进行加密,然后将其保存在txt文件中。基本上我需要将字节数组转换为inkcanvas笔划。我完成了代码的前半部分(将inkcanvas笔划转换为字节数组):

for %%f in (*.py) do (
    start %%f
)

但是我需要帮助编写一个方法来将字节数组转换为inkcanvas笔划,以便将inkcanvas笔划转换为jpg。

到目前为止,我已经创建了一个方法,它打开字节数组文件并将其写入字节数组变量:

    private byte[] InkCanvasToByte()
    {
        using (MemoryStream ms = new MemoryStream())
        {
            if(myInkCanvas.Strokes.Count > 0)
            {
                myInkCanvas.Strokes.Save(ms, true);
                byte[] unencryptedSignature = ms.ToArray();
                return unencryptedSignature;
            }
            else
            {
                return null;
            }
        }
    }

现在我需要做的就是通过inkcanvas笔划将该字节数组转换回图像。如果我找到一个解决方案,我会用解决方案更新这篇文章!

编辑:嗯。我使用该链接中的代码得到:&#34;输入流不是有效的二进制格式。起始内容(在旁边)是:00-FB-03-03-06-48-11-45-35-46-35-11-00-00-80-3F-1F ...&#34; < / p>

我使用的代码是:

    private void ReadByteArrayFromFile()
    {
        string Chosen_File = "";
        Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
        ofd.Filter = "All Files (*.*)|*.*";
        ofd.FilterIndex = 1;
        ofd.Multiselect = false;
        bool? userClickedOK = ofd.ShowDialog();
        if (userClickedOK == true)
        {
            Chosen_File = ofd.FileName;
        }
        byte[] bytesFromFile = File.ReadAllBytes(Chosen_File);

    }

}

1 个答案:

答案 0 :(得分:0)

我的问题是我没有将输出序列化为保存的文件,因此我加载该文件时反序列化它会导致错误。这是正确的代码:

    private void SaveByteArrayToFile(byte[] byteArray)
    {
        var dialog = new System.Windows.Forms.FolderBrowserDialog();
        string filepath = "";
        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            filepath += dialog.SelectedPath;
            System.Windows.MessageBox.Show(filepath);
        }
        filepath += "Signature.txt";
        MyCustomStrokes customStrokes = new MyCustomStrokes();
        customStrokes.StrokeCollection = new Point[myInkCanvas.Strokes.Count][];
        for (int i = 0; i < myInkCanvas.Strokes.Count; i++)
        {
            customStrokes.StrokeCollection[i] = 
            new Point[this.myInkCanvas.Strokes[i].StylusPoints.Count];
            for (int j = 0; j < myInkCanvas.Strokes[i].StylusPoints.Count; j++)
            {
                customStrokes.StrokeCollection[i][j] = new Point();
                customStrokes.StrokeCollection[i][j].X = 
                                  myInkCanvas.Strokes[i].StylusPoints[j].X;
                customStrokes.StrokeCollection[i][j].Y = 
                                  myInkCanvas.Strokes[i].StylusPoints[j].Y;
            }
        }
        MemoryStream ms = new MemoryStream();
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(ms, customStrokes);
        File.WriteAllBytes(filepath, Encrypt(ms.GetBuffer()));
    }
   private void ReadByteArrayFromFile(string Chosen_File)
    {

        byte[] bytesFromFile = File.ReadAllBytes(Chosen_File);
        byte[] decryptedBytes = Decrypt(bytesFromFile);
        try
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream(decryptedBytes);
            MyCustomStrokes customStrokes = bf.Deserialize(ms) as MyCustomStrokes;
            for(int i = 0; i < customStrokes.StrokeCollection.Length; i++)
            {
                if(customStrokes.StrokeCollection[i] != null)
                {
                    StylusPointCollection stylusCollection = new
                      StylusPointCollection(customStrokes.StrokeCollection[i]);
                    Stroke stroke = new Stroke(stylusCollection);
                    StrokeCollection strokes = new StrokeCollection();
                    strokes.Add(stroke);
                    this.MyInkPresenter.Strokes.Add(strokes);
                }
            }

        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message);
        }
    }
[Serializable]
public sealed class MyCustomStrokes
{
    public MyCustomStrokes() { }
    /// <SUMMARY>
    /// The first index is for the stroke no.
    /// The second index is for the keep the 2D point of the Stroke.
    /// </SUMMARY>
    public Point[][] StrokeCollection;
}