我正在使用下面的函数将我在图片框控件上绘制的一些墨水形状转换为 Base64
Function GetBase64(ByVal InkCollector As InkCollector) As String
Dim utf8 As UTF8Encoding = New UTF8Encoding()
InkCollector.Enabled = False
Return utf8.GetString(InkCollector.Ink.Save(PersistenceFormat.Base64InkSerializedFormat, CompressionMode.Maximum))
InkCollector.Enabled = True
End Function
然后,我可以使用该字符串将草图复制到新图像中,方法是将其提供给此函数:
Function Base64toImage(ByVal Base64 As String) As System.Drawing.Image
Dim utf8 As UTF8Encoding = New UTF8Encoding()
Dim imgSig As System.Drawing.Image
Dim tmploadedInk As Ink = New Ink()
Dim strGIF As String
Dim imageBytes() As Byte
Dim MS As MemoryStream
'Load the Base64 String in Format(PersistenceFormat = Base64InkSerializedFormat) as ink
tmploadedInk.Load(utf8.GetBytes(Base64))
'Convert the ink to Base64 String in format (PersistenceFormat.Gif, CompressionMode.Maximum)
strGIF = Convert.ToBase64String(tmploadedInk.Save(PersistenceFormat.Gif, CompressionMode.Maximum))
'Convert Base64 String to Byte Array
imageBytes = Convert.FromBase64String(strGIF)
MS = New MemoryStream(imageBytes, 0, imageBytes.Length)
' Convert byte[] to Image
MS.Write(imageBytes, 0, imageBytes.Length)
imgSig = Image.FromStream(MS, True)
Return imgSig
End Function
除了形状被裁剪并因此它们与生成图像的左上角对齐之外,这很好用。如果我使用如下代码行:
PictureBox1.Image = Signiture.Base64toImage(Signiture.GetBase64(Sketch_InkCollector))
这些形状出现在左上角,忽略了它们可能被绘制在原始图片框的中心的事实。我相信定位数据已正确保存到 Base64 字符串中,但在将形状传输到图像变量时会以某种方式被忽略。我相信这是因为当我将下面的函数与 Base64 字符串一起使用时,形状会正确放置在其原始位置。
Sub SetBase64PictureBox(ByVal InkCollector As InkCollector, ByVal PictureBox As PictureBox, ByVal Base64 As String)
Dim loadedInk As Ink = New Ink()
Dim utf8 As UTF8Encoding = New UTF8Encoding()
InkCollector.Enabled = False
InkCollector.Ink.DeleteStrokes() ' Clear all strokes
loadedInk.Load(utf8.GetBytes(Base64))
InkCollector.Ink = loadedInk
InkCollector.Enabled = True
PictureBox.Invalidate()
End Sub
谢谢
更新:
在此链接中找到了一个临时解决方案:
InkPicture 控件 - 如何将墨水和图像保存到图像文件
这不完全是我所追求的,但最终结果是相同的,因为我想将图片和墨迹组合成图像以显示在 PDF 文件中。