Converting InkCanvas Strokes to a Byte Array and back again

3.2k views Asked by At

I am working on a program which converts the inkcanvas strokes to a byte array for encryption and then saves it in a txt file. Essentially I need to convert a byte array to inkcanvas strokes. I have the first half of the code done (which converts the inkcanvas strokes to a byte array):

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

But I need help writing a method to convert the byte array into inkcanvas strokes in order to convert the inkcanvas strokes to a jpg.


So far I have created a method which opens the byte array file and writes it to a byte array variable:

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

    }

Now all I need to do is convert that byte array back into an image, either through inkcanvas strokes. I'll update this post with a solution if I find one!

EDIT: Hmm. I'm using the code from that link and I get: "The input stream is not a valid binary format. The Starting contents (in byes) are: 00-FB-03-03-06-48-11-45-35-46-35-11-00-00-80-3F-1F ..."

The code I'm using is:

    private void ReadByteArrayFromFile(string Chosen_File)
    {

        byte[] bytesFromFile = File.ReadAllBytes(Chosen_File);
        try
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream(bytesFromFile);
            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);
        }
    }

    private void DecryptByteArray(byte[] encryptedArray)
    {
    }


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

}

1

There are 1 answers

0
gtnewhouse On BEST ANSWER

My problem was that I didn't serialize the output to the saved file and thus the when I loaded that file deserializing it tripped an error. Here is the correct code:

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