C# 将InkCanvas笔划转换为字节数组并再次转换

C# 将InkCanvas笔划转换为字节数组并再次转换,c#,wpf,bytearray,converter,inkcanvas,C#,Wpf,Bytearray,Converter,Inkcanvas,我正在开发一个程序,该程序将inkcanvas笔划转换为字节数组进行加密,然后将其保存在txt文件中。本质上,我需要将字节数组转换为inkcanvas笔划。我已经完成了代码的前半部分(它将inkcanvas笔划转换为字节数组): 但是我需要帮助编写一个将字节数组转换为inkcanvas笔划的方法,以便将inkcanvas笔划转换为jpg 到目前为止,我已经创建了一个方法,可以打开字节数组文件并将其写入字节数组变量: private void ReadByteArrayFromFile(

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

但是我需要帮助编写一个将字节数组转换为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;
}
private void ReadByteArrayFromFile(选择字符串\u文件)
{
byte[]bytesFromFile=File.ReadAllBytes(所选文件);
尝试
{
BinaryFormatter bf=新的BinaryFormatter();
MemoryStream ms=新的MemoryStream(bytesFromFile);
MyCustomStrokes customStrokes=bf。反序列化(ms)为MyCustomStrokes;
对于(int i=0;i

}

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

    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;
}
private void savebytearray文件(字节[]byteArray)
{
var dialog=new System.Windows.Forms.FolderBrowserDialog();
字符串filepath=“”;
if(dialog.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
filepath+=dialog.SelectedPath;
System.Windows.MessageBox.Show(文件路径);
}
filepath+=“Signature.txt”;
MyCustomStrokes customStrokes=新建MyCustomStrokes();
customStrokes.StrokeCollection=新点[myInkCanvas.Strokes.Count]];
for(int i=0;i
尝试查看以下内容:。看起来可能会有帮助。Jashaszun,谢谢!我看到了,决定在午饭前检查一下。我会更新帖子并回答我的问题,如果它有效的话。WPF不是这样工作的。获取位图的唯一方法是从WPF可显示对象(在本例中为画布、演示器等)开始,并将其渲染为例如
RenderTargetBitmap
。换句话说,“如何保存/加载笔划”的问题与“如何将笔划渲染为位图”的问题完全不同。请找出您需要帮助的问题,并编辑/发布问题
    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;
}