Results 1 to 7 of 7

Thread: Inserting hex string into RichtextBox as Image

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Inserting hex string into RichtextBox as Image

    I have a base64 encoded string that I need to convert to an image and insert it into a RichtextBox. First I convert the string into a byte array then into a hex string, then I try to insert the hex string into the RTF but I keep getting the error that "file format is not valid" (see first code block for complete error). I need to do this WITHOUT saving the image data to a file. I did add the temporary line to write the image to a file to make sure the file data was valid and it does work and I can open and view the image in MSPaint with no issues. However, because this image does not exist as an actual image file I can't use the normal solution I find everywhere of copying the image to clipboard.

    Code:
    An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
    
    Additional information: File format is not valid.
    Here is some sample code and the last 4 lines represent 4 of the about 30+ different variations I have tried. Sorry I didn't save every edit.

    Code:
            Dim b As Byte() = Convert.FromBase64String(TextBox2.Text)
            File.WriteAllBytes("zzzTest.jpg", b)
            Dim hex As String = BitConverter.ToString(b)
            hex = hex.Replace("-", "")
    'Samples of what I have tried:
            RichTextBox1.Rtf = "{\pict\jpegblip " & hex & " }"
            RichTextBox1.Rtf = "{\pict\jpegblip\hex " & hex & " }"
            RichTextBox1.Rtf = "{{\pict\jpegblip\pich740\picw640\hex " & hex & " }"
            RichTextBox1.Rtf = "{\*\shppict{\pict\jpegblip\pich740\picw640\hex " & hex & " }"

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Inserting hex string into RichtextBox as Image

    If you want to write bytes to a JPG file then those bytes have to actually be in JPG format, otherwise the file will be useless. Setting the extension of a file name doesn;t magically change the contents, any more than writing "Sugar" on a jar containing salt would make eating a spoonful of the contents a pleasurable experience. If you want to create an image file then you need to save image data.

    In your case, the way to do that would be to create a Bitmap object of the appropriate size, draw your text onto that using GDI+, then save that object to a file. Those are all things that you can research fairly easily.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: Inserting hex string into RichtextBox as Image

    Quote Originally Posted by jmcilhinney View Post
    If you want to write bytes to a JPG file then those bytes have to actually be in JPG format, otherwise the file will be useless. Setting the extension of a file name doesn;t magically change the contents, any more than writing "Sugar" on a jar containing salt would make eating a spoonful of the contents a pleasurable experience. If you want to create an image file then you need to save image data.

    In your case, the way to do that would be to create a Bitmap object of the appropriate size, draw your text onto that using GDI+, then save that object to a file. Those are all things that you can research fairly easily.
    First, the data is in fact in proper jpg format, as I said if I save it to a file it is a proper jpg and opens as such. The issue is I do NOT want a physical file, I need to display the data as an image without using an intermediary file.

    Maybe it would help if I explained what I am doing overall. I am trying to write a basic email reader that opens the .msg file and displays the contents in the RTB. The image is INLINE data as a base64 string. So I need to read that string, convert it to bytes (which works so far) then display the image in the RTB. I will also need to be able to properly display the html as well (I may just strip that out and leave the text only) but that will be the next step.

    Now I know at this point most of you are going to say to either just use an existing mail reader or use the WebControl but neither of these options are acceptable. The purpose of this mail reader is to investigate suspicious emails (emails with high probability to contain malicious code) and as such I cannot use anything that might allow any sort of scripting or activeX controls to run or that can possibly make a connection to an internet resource. In other words, I need to see what is in them without allowing anything to be executed. Thus I cannot use any standard mail reader and why I am using RTB instead of WebControl.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Inserting hex string into RichtextBox as Image

    If you have the byte array, I believe you should be able to write the byte array to a memory stream, and load the image in a picturebox from the memory stream.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: Inserting hex string into RichtextBox as Image

    Quote Originally Posted by passel View Post
    If you have the byte array, I believe you should be able to write the byte array to a memory stream, and load the image in a picturebox from the memory stream.
    So it's just not possible to add it to the RTB then? At least not without saving the image to a file first?

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Inserting hex string into RichtextBox as Image

    A memory stream isn't a file.
    I'm not familiar with adding an image to a richtextbox, so perhaps it is more involved than the case where I've used a memory stream to transfer an byte array of an image format (received over tcp) to a picturebox. I would think you can create an image from a memory stream, and once you have an image, it should be addable to a richtextbox, but I admit I don't usually work with richtextboxes, so can't state that definitively.

    In my case, I have a byte array name bData that contains a png or jpg image that was transferred over tcp, I have a memory stream object, write the byte array to it, and then use the Bitmap constructor method to create the bitmap from the stream and assign it to the picturebox.
    To work with a RichTextbox would require some research on my part since I haven't worked with it, and I don't have time to investigate that now.
    Code:
           mstream = New System.IO.MemoryStream       '    Create a memory stream
           mstream.Write(bData, 0, bData.Length)      '    Write the image data to the memory stream
    
           With PictureBox1
            If .Image IsNot Nothing Then .Image.Dispose() 'If we have an image in the picturebox, dispose of it first
            .Image = New Bitmap(mstream)                  'Create a new image from the memorystream provided by the TCP receiver
          End With
    
          mstream.Dispose()                          '    Dispose of the memory stream (now that it has been read)

  7. #7
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Inserting hex string into RichtextBox as Image

    Quote Originally Posted by Maverickz View Post
    So it's just not possible to add it to the RTB then? At least not without saving the image to a file first?
    Yes it is possible ... There are lots of eg's out there ... but most use copy & paste to do it (AVOID THESE)...
    I saw a solution on CP that does it properly ... will have a look later if I have the chance.

    EDIT:
    ... thought I'd have a quick look now to see if I could find it ... ...
    https://www.codeproject.com/Articles...chTextBox-at-R

    Kris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width