【问题标题】:Converting InkCanvas Strokes to a Byte Array and back again将 InkCanvas 笔划转换为字节数组并再次返回
【发布时间】:2015-09-02 23:01:00
【问题描述】:

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

    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 笔划的方法,以便将 inkcanvas 笔划转换为 jpg。


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

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

    }

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

编辑:嗯。我正在使用该链接中的代码,我得到:“输入流不是有效的二进制格式。起始内容(以字节为单位)是:00-FB-03-03-06-48-11-45-35 -46-35-11-00-00-80-3F-1F ..."

我使用的代码是:

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

}

【问题讨论】:

  • 试试看这个:Saving-Rebuilding InkCanvas Strokes。看起来它可能会有所帮助。
  • Jashaszun,谢谢!我看到了,并决定在午餐前检查一下。如果可行,我会更新帖子并回答我的问题。
  • 这不是 WPF 的工作方式。获取位图的唯一方法是从 WPF 可显示对象(在本例中为画布、演示者等)开始并将其渲染为例如RenderTargetBitmap。换句话说,“我如何保存/加载笔画”的问题与“如何将笔画渲染成位图”的问题完全分开。请找出您需要帮助的问题,并编辑/发布一个问题以包含a good, minimal, complete code example,清楚地显示您尝试过的内容以及遇到的问题。
  • 好的,彼得,我的问题仅限于将字节数组文件转换为笔画。

标签: c# wpf bytearray converter inkcanvas


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-06-23
    • 2010-11-28
    • 2013-10-14
    • 1970-01-01
    • 2015-01-19
    • 2013-02-18
    • 2011-01-13
    • 1970-01-01
    • 2012-07-18
    相关资源
    最近更新 更多