locked
Btye array to a Rich Text Box RRS feed

  • Question

  • Hi All,

    Currently, I am creating RTF documents with MS Word that also contain images. I use the LoadFile(Filename, RichTextBoxStreamType.RichText) method to get the document in the Rich Text Box.

    This works well, but is not the way I need to do it.

    Lets just say that the entire RTF document, including images, is in a one dimensional byte array. I need to load this byte array into the rich text box as if it were a file loaded from disk.

    Any assistance is greatly appreciated.

    DW

    Monday, September 11, 2017 2:08 PM

Answers

  • Hi All,

    Currently, I am creating RTF documents with MS Word that also contain images. I use the LoadFile(Filename, RichTextBoxStreamType.RichText) method to get the document in the Rich Text Box.

    This works well, but is not the way I need to do it.

    Lets just say that the entire RTF document, including images, is in a one dimensional byte array. I need to load this byte array into the rich text box as if it were a file loaded from disk.

    Any assistance is greatly appreciated.

    DW

    DW,

    Just "off the cuff" here but you might try this:

    Load your Byte Array into a memorystream. For example:

    Dim byteArray() As Byte = Nothing
    
    Using ms As New IO.MemoryStream(byteArray)
        ms.Position = 0
    
        ' Rest of the code here
    End Using

    Inside the Using block, you can use the RTB's overload for loading from a stream:

    https://msdn.microsoft.com/en-us/library/ms160332(v=vs.110).aspx

    Untested but that's the idea. Obviously don't set the byte array to Nothing - I just did that to satisfy the compiler. ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by dw80916 Monday, September 11, 2017 6:29 PM
    Monday, September 11, 2017 2:18 PM

All replies

  • Hi All,

    Currently, I am creating RTF documents with MS Word that also contain images. I use the LoadFile(Filename, RichTextBoxStreamType.RichText) method to get the document in the Rich Text Box.

    This works well, but is not the way I need to do it.

    Lets just say that the entire RTF document, including images, is in a one dimensional byte array. I need to load this byte array into the rich text box as if it were a file loaded from disk.

    Any assistance is greatly appreciated.

    DW

    DW,

    Just "off the cuff" here but you might try this:

    Load your Byte Array into a memorystream. For example:

    Dim byteArray() As Byte = Nothing
    
    Using ms As New IO.MemoryStream(byteArray)
        ms.Position = 0
    
        ' Rest of the code here
    End Using

    Inside the Using block, you can use the RTB's overload for loading from a stream:

    https://msdn.microsoft.com/en-us/library/ms160332(v=vs.110).aspx

    Untested but that's the idea. Obviously don't set the byte array to Nothing - I just did that to satisfy the compiler. ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by dw80916 Monday, September 11, 2017 6:29 PM
    Monday, September 11, 2017 2:18 PM
  • Thanks,

    Will give that a try.

    Monday, September 11, 2017 2:28 PM
  • Thanks,

    Will give that a try.

    Just to make sure that it worked (it does), the following is pointless but does show that it took a file which is saved from rich text (from MS Word) and from that, created a byte array. It then took the byte array and read it as a stream in a MemoryStream object.

    Inside that Using block is another Using block that creates a RichTextBox so that I can then use the .Load method that I linked to earlier. I then get the RTF from it and return it.

    Then I show it in a RichTextBox on the form -- like I said, pretty pointless other than to prove that it works. ;-)

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub _ Form1_Load(sender As System.Object, _ e As System.EventArgs) Handles MyBase.Load Dim desktop As String = _ Environment.GetFolderPath(Environment.SpecialFolder.Desktop) Dim filePath As String = _ IO.Path.Combine(desktop, "VBForum3.rtf") Dim rtfString As String = GetRTF(filePath) If Not String.IsNullOrWhiteSpace(rtfString) Then RichTextBox1.Rtf = rtfString End If End Sub Private Function GetRTF(ByVal filePath As String) As String Dim retVal As String = Nothing If Not String.IsNullOrWhiteSpace(filePath) Then Dim fi As New IO.FileInfo(filePath) If fi.Exists Then Dim b() As Byte = IO.File.ReadAllBytes(fi.FullName) retVal = GetRTF(b) End If End If Return retVal End Function Private Function GetRTF(ByVal bytes() As Byte) As String Dim retVal As String = Nothing If bytes IsNot Nothing AndAlso bytes.Length > 0 Then Using ms As New IO.MemoryStream(bytes) Using rtb As New RichTextBox rtb.LoadFile(ms, RichTextBoxStreamType.RichText) retVal = rtb.Rtf End Using End Using End If Return retVal End Function End Class



    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, September 11, 2017 3:11 PM
  • Try the next approach too:

    Dim bytes() As Byte = . . .
    
    RichTextBox1.Rtf = Encoding.ASCII.GetString(bytes)
    

    Monday, September 11, 2017 5:06 PM
  • This works well. Thanks for the assistance.
    Using ms As New IO.MemoryStream(ByteArray)
        ms.Position = 0
        Form1.rtb.LoadFile(ms, RichTextBoxStreamType.RichText)
    End Using

    Monday, September 11, 2017 6:29 PM
  • This works well. Thanks for the assistance.
    Using ms As New IO.MemoryStream(ByteArray)
        ms.Position = 0
        Form1.rtb.LoadFile(ms, RichTextBoxStreamType.RichText)
    End Using

    I'm glad it helped. :)

    You don't need to set the position to zero. Like I said, it was "off the cuff".

    It's being read after it's loaded -- I misstated it, but just loading the stream doesn't change the position (that's what I meant anyway!). It's being read when the RTB asks for it using the .Load method that you did and at that point the position is moved.

    It doesn't hurt to leave it there, but it's not doing anything. ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Monday, September 11, 2017 6:35 PM ...reworded
    Monday, September 11, 2017 6:32 PM