将Ink转换为Base64然后再转换为Image会将形状裁剪为

时间:2016-04-27 07:23:51

标签: vb.net image base64 microsoft.ink

我正在使用下面的函数将我在picturebox控件上绘制的一些墨迹形状转换为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 control - How to save both ink and image to an image file

这不完全是我所追求的,但最终结果是相同的,因为我想将组合图片和墨迹描边成图像以显示在PDF文件中。

0 个答案:

没有答案