- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一个程序,将 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;
}
最佳答案
我的问题是我没有将输出序列化到保存的文件中,因此当我加载该文件时反序列化它会触发错误。这是正确的代码:
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;
}
关于c# - 将 InkCanvas 笔划转换为字节数组并再次转换回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30896167/
我使用具有固定宽度和高度的 Canvas 。它有 500x500 大,我也想用这种方法保存它: private async void SaveasGIF(object sender, Rout
我希望能够在 InkCanvas 上绘制形状。到目前为止,我有以下 XAML:- 在页面构造函数中,我有以下内容:- inker.InkPresenter.Unprocesse
在我的申请中,我有一个 InkCanva s 在背景中有图像。当我在 InkCanvas 上画画时到底部或左侧边缘,InkCanvas更改大小以适合草图,这会扰乱背景中图像的渲染。如何停止 InkCa
所以我试图通过使用套接字创建一个类似同步绘画程序的东西。我有一个服务器端..和一个客户端,我试图将inkCollection从服务器发送到客户端。这适用于简单的文本,但我似乎无法发送inkCollec
我正在为触摸屏创建一个简单的绘画应用程序,我一直在与多点触控手势进行一些斗争,以便在 inkcanvas 中缩放和旋转,但我还没有找到解决方案。 有没有一种在 inkcanvas 中提供多点触控手势的
我正在为触摸屏创建一个简单的绘画应用程序,我一直在与多点触控手势进行一些斗争,以便在 inkcanvas 中缩放和旋转,但我还没有找到解决方案。 有没有一种在 inkcanvas 中提供多点触控手势的
我正在使用 InkCanvas 创建类似绘画的应用程序,我愿意实现撤消和我的应用程序中的重做功能。 哪一种是为 InkCanvas 实现撤消/重做的最佳方式? 最佳答案 我已经为 WPF 应用程序实现
使用以下 XAML: Te
我以前从未使用过InkCanvas 控件。我需要的是将文件加载到 InkCanvas 中,绘制一些涂鸦并获得生成的图像。我想对获取的图像进行一些额外的操作。 至于节省 如果我错了请纠正我。我找到了一个
在 WinForms 的 Microsoft.Ink 世界中,使用 InkCollector 对象,用户可以通过 this approach 获取笔画中某个点的时间戳. WPF 世界依赖于 InkCa
我正在开发一个程序,将 inkcanvas 笔画转换为字节数组进行加密,然后将其保存在 txt 文件中。本质上,我需要将字节数组转换为 inkcanvas 笔划。我已经完成了前半部分代码(将 inkc
我目前正在开发一个 UWP 应用程序,它应该允许用户在 InkCanvas 上绘制他想要的任何形状。这个形状的边缘是自动完成的,因此没有开放空间(到目前为止一切正常)。 现在我的问题是:如何使用单一颜
我将 WPF InkCanvas 用于绘图应用程序。 Canvas 位于图像上方。我可以在图像上涂鸦,但我需要它有发光效果——即实际绘制的线条必须有这种效果。有没有办法在 XAML 或 C# 代码中执
我正在使用 MVVMLight 创建问卷,并在呈现 InkCanvas 控件时遇到内存问题。这是我正在使用的一个淡化示例: 问题Vm public Question Question { get; s
在我的 UWP 应用程序中,我试图获取作为 InkCanvas 中墨迹笔画一部分的每个点的坐标,但官方文档似乎没有谈论它,有谁知道如何做到这一点? 最佳答案 只是猜测,但从文档来看它看起来像: for
我在一个窗口上有一个 InkCanvas,我允许用户在其中使用手写笔、触摸或鼠标进行绘图。我还允许用户添加文本。用户点击“添加文本”按钮,然后点击 Canvas 上他们想要文本的位置。那里会出现一个文
在我的 WPF 应用程序中,我有一些绘图功能。我已经使用 Canvas 解决了这个问题,并手动处理了鼠标手势,我还将绘制的 Strokes(包装在 InkPresenter 中)添加到了这个 Canv
您好,我不知道为什么它不起作用。看起来我不能在 InkCanvas 上使用这个事件。 XAML Clear 代码隐藏 private void MC_MouseLeftButt
我正在 UWP 中开发一个绘图程序,希望我的程序支持多个手写笔输入。当我在 WPF 中开发时,在进行了一些巧妙的鼠标和手写笔事件处理后,我能够从 WPF 版本的 InkCanvas 获得多个手写笔输入
是否可以将 InkCanvas 笔划集合保存为 svg 图像?我唯一能找到的是我可以将笔画保存为带有嵌入式 ISF(墨水序列化格式)的 GIF,或者将它们渲染为位图。我想以可以与其他平台(如网络)互操
我是一名优秀的程序员,十分优秀!