如何在VB.net中的RTF控件中显示Bmp

如何在VB.net中的RTF控件中显示Bmp,vb.net,rtf,bmp,Vb.net,Rtf,Bmp,我是从这个开始的 我正在尝试在rtf框中显示bmp图像,用于我正在制作的机器人程序。 此函数用于将位图转换为rtf代码,同时插入另一个rtf格式化程序srtring和其他文本。有点像聊天程序中使用的笑脸 由于某种原因,此函数的输出被RTF框拒绝并完全消失。我不确定它是否与我将bmp转换为二进制字符串的方式相同,或者它是否与标题标记绑定在一起 lb.SelectedRtf=FormatText(build.ToString,newColor) 从来没有找到我希望的解决办法。。但我确实找到了工作

我是从这个开始的

我正在尝试在rtf框中显示bmp图像,用于我正在制作的机器人程序。 此函数用于将位图转换为rtf代码,同时插入另一个rtf格式化程序srtring和其他文本。有点像聊天程序中使用的笑脸

由于某种原因,此函数的输出被RTF框拒绝并完全消失。我不确定它是否与我将bmp转换为二进制字符串的方式相同,或者它是否与标题标记绑定在一起

lb.SelectedRtf=FormatText(build.ToString,newColor)


从来没有找到我希望的解决办法。。但我确实找到了工作

 'returns the RTF string representation of our picture
    Public Shared Function PictureToRTF(ByVal Bmp As Bitmap) As String

    'Create a new bitmap
    Dim BmpNew As New Bitmap(Bmp.Width, Bmp.Height, Imaging.PixelFormat.Format24bppRgb)
    Dim gr = Graphics.FromImage(BmpNew)
    gr.DrawimageUnscaled(Bmp, 0, 0)
    gr.dispose()

    Dim stream As New MemoryStream()
    BmpNew.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)

        Dim bytes As Byte() = stream.ToArray()

        Dim str As String = BitConverter.ToString(bytes, 0).Replace("-", String.Empty)

        'header to string we want to insert
        Using g As Graphics = Main.CreateGraphics()
            xDpi = g.DpiX
            yDpi = g.DpiY
        End Using

        Dim _rtf As New StringBuilder()

        ' Calculate the current width of the image in (0.01)mm
        Dim picw As Integer = CInt(Math.Round((Bmp.Width / xDpi) * HMM_PER_INCH))

        ' Calculate the current height of the image in (0.01)mm
        Dim pich As Integer = CInt(Math.Round((Bmp.Height / yDpi) * HMM_PER_INCH))

        ' Calculate the target width of the image in twips
        Dim picwgoal As Integer = CInt(Math.Round((Bmp.Width / xDpi) * TWIPS_PER_INCH))

        ' Calculate the target height of the image in twips
        Dim pichgoal As Integer = CInt(Math.Round((Bmp.Height / yDpi) * TWIPS_PER_INCH))

        ' Append values to RTF string
        _rtf.Append("{\pict\wbitmap0")
        _rtf.Append("\picw")
        _rtf.Append(Bmp.Width.ToString)
        '  _rtf.Append(picw.ToString)
        _rtf.Append("\pich")
        _rtf.Append(Bmp.Height.ToString)
        ' _rtf.Append(pich.ToString)
        _rtf.Append("\wbmbitspixel24\wbmplanes1")
        _rtf.Append("\wbmwidthbytes40")
        _rtf.Append("\picwgoal")
        _rtf.Append(picwgoal.ToString)
        _rtf.Append("\pichgoal")
        _rtf.Append(pichgoal.ToString)
        _rtf.Append("\bin ")

        _rtf.Append(str.ToLower & "}")
        Return _rtf.ToString
    End Function

Public Function FormatText(ByVal data As String, ByVal newColor As fColorEnum) As String
    data = System.Net.WebUtility.HtmlDecode(data)
    data = data.Replace("|", " ")
    Dim reg As New Regex("\$(.[0-9]+)\$")
    If reg.IsMatch(data) Then
        Dim meep As String = Regex.Match(data, "\$(.[0-9]+)\$").Groups(1).ToString
        Dim idx As Integer = Convert.ToInt32(meep)
        Dim img As String = Fox2RTF(idx)

        If img IsNot Nothing Then data = Regex.Replace(data, "\$(.[0-9]+)\$", img)
    End If
    Dim myColor As System.Drawing.Color = fColor(newColor)
    Dim ColorString = "{\colortbl ;"
    ColorString += "\red" & myColor.R & "\green" & myColor.G & "\blue" & myColor.B & ";}"
    Dim FontSize As Integer = cMain.ApFont.Size
    Dim FontFace As String = cMain.ApFont.Name
    FontSize *= 2
    Dim test As String = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033" & ColorString & "{\fonttbl{\f0\fcharset0 " & FontFace & ";}}\viewkind4\uc1\fs" & FontSize.ToString & data & "\par}"
    Return "{\rtf1\ansi\ansicpg1252\deff0\deflang1033" & ColorString & "{\fonttbl{\f0\fcharset0 " & FontFace & ";}}\viewkind4\uc1\fs" & FontSize.ToString & data & "\cf0 \par}"
End Function
Private Function Fox2RTF(ByRef Img As Integer) As String
    Dim shape As New FurcadiaShapes(Paths.GetDefaultPatchPath() & "system.fsh")
    Dim anims As Bitmap() = Helper.ToBitmapArray(shape)
    ' pic.Image = anims(Img)

    Return PictureToRTF.PictureToRTF(anims(Img))

End Function