none
how can i add a photo to richtextbox and from there i want to save it a rtf file.

    Question


  •     Private Sub lblsave_Click(sender As Object, e As EventArgs) Handles lblsave.Click

            '// to save new data

            If MsgBox("Do you want to save it permanently.", MsgBoxStyle.YesNo, "Save") = MsgBoxResult.Yes Then
                MsgBox("Saved", MsgBoxStyle.Information)


                saveToday.Write(vbCrLf & vbCrLf & dtp.Text & vbCrLf & vbCrLf & "Feelings :" & cbf.Text & vbCrLf & "Weather :" & cbw.Text & vbCrLf & vbCrLf & txtmain.Text & vbCrLf & vbCrLf & "_____________________________________________________________________")
                saveToday.Close()

            Else
                txtmain.Focus()
            End If

        End Sub
    • Edited by syed.bcc Thursday, April 20, 2017 8:14 PM
    Thursday, April 20, 2017 8:12 PM

All replies

  • Hi

    Here is code to show one way to do it.

    This example will open Form1 with some text, followed by a picture and then some more text. You would need to edit the Path variable and the filenames to test this code.

    There are two buttons, one just clears the RTB the other reloads the saved RTF file.

    Option Strict On
    Option Explicit On
    Public Class Form1
        ' edit this line for a Path to an image
        Dim Path As String = "C:\Users\Les\Desktop\Dice 2\"
    
        ' edit this line for an image filename
        Dim img As Image = Image.FromFile(Path & "11.png")
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
            RichTextBox1.AppendText("This is a complete illusion" & vbCrLf)
    
            Dim current As IDataObject = Clipboard.GetDataObject
            Clipboard.SetImage(img)
            RichTextBox1.Paste()
            Clipboard.SetDataObject(current)
            RichTextBox1.AppendText(vbCrLf & "This, on the other hand; isn't!")
    
            ' save the RTF to file in same path as image
            ' change to suit (change below as well)
            RichTextBox1.SaveFile(Path & "DummyRTF.rtf", RichTextBoxStreamType.RichText)
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' this button click will erase everything in the RTB
            RichTextBox1.Clear()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ' this button click will reload from saved file (should result in exactly the same contents)
            RichTextBox1.LoadFile(Path & "DummyRTF.rtf", RichTextBoxStreamType.RichText)
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    Thursday, April 20, 2017 10:09 PM
  • thanks for your quick reply, but...

    it would be difficult to change the code easily as i'm just a newbie programmer from sixth form, do before doing this could you tell me that how can i enable a user to import a image to the RTB (He can also write text there as well) and then by clicking the save button he can also save and reload the .rtf file any time with those image and text.

    its actually a personal diary so i want to allow the user to almost add any possible thing to it and also can save for later to reload/see.


    syed.bcc

    Friday, April 21, 2017 7:19 PM
  • thanks for your quick reply, but...

    it would be difficult to change the code easily as i'm just a newbie programmer from sixth form, do before doing this could you tell me that how can i enable a user to import a image to the RTB (He can also write text there as well) and then by clicking the save button he can also save and reload the .rtf file any time with those image and text.

    its actually a personal diary so i want to allow the user to almost add any possible thing to it and also can save for later to reload/see.


    syed

    Hi

    Sorry, but you have already been given an answer. It answers the exact question you are asking again!

    In order for a user to import an image, there needs to be a path to the image - in the example, I hard coded a path but that can be done better by using an OpenFileDialog control which lets the user browse to where the image is and to select it. Once the user has selected a file to import, then your code need to add it to the RTB (at the insertion point where the cursor is) - this is done by the block of code

     Dim current As IDataObject = Clipboard.GetDataObject
     Clipboard.SetImage(img)
     RichTextBox1.Paste()
     Clipboard.SetDataObject(current)

    The first line stores whatever might be in the clipboard before adding the image to the clipboard and then pasting the image into the RTB followed by restoring the old clipboard contents.

    The line

     RichTextBox1.SaveFile(Path & "DummyRTF.rtf", RichTextBoxStreamType.RichText)

    saves the entire RTB with text and images, and the line

     RichTextBox1.LoadFile(Path & "DummyRTF.rtf", RichTextBoxStreamType.RichText)
    will load the contents of an RTF file, entire, into a RTB.

    I am not aware of any other more simple way to do what you are asking.

    '

    BTW. the rest of the code in my post above is purely to build the example.



    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, April 21, 2017 9:56 PM
    Friday, April 21, 2017 9:56 PM
  • its actually a personal diary so i want to allow the user to almost add any possible thing to it and also can save for later to reload/see.

    If you want more assistance with your application you will need to provide more detail on how you want it to work, and you will need to break the application down into separate tasks.

    The code you have allows you to import an image into a rich text box, with text.

    If you want to allow the user to provide the text then you can remove the part that inserts text and just use the text input facility of the RTB - that part should be straightforward.  But you need to indicate how the user is going to indicate where the image is to be inserted, and how they will select the image to be inserted.   Generally, the user will indicate the position to insert the image by moving the selection point (the caret), but your application might require something different (eg, always at the end of the existing text).  The image could be selected from a list of names, from a list of images, from a file on disk, or something else.   What selection procedure will you use for your application?  You need to get that working before worrying about the rest.

    Friday, April 21, 2017 10:02 PM