La Web del Programador: Comunidad de Programadores
 
    Pregunta:  48217 - COMO INSERTAR UNA IMAGEN DESDE UN FICHERO AL RICHTEXBOX
Autor:  Jenis Chirino
No sabemos como implementar la inserción de una imagen en el RichTexBox cogiéndola desde un fichero en C#.
Gracias

  Respuesta:  Don camilo
Hola. ya habia dicho yo q lo habia en VB.
Si a alguien le sirve paso a explicar:

supongamos tengo un boton para llamar la funcion
<code>

Private Sub btnReporteTextoEditable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReporteTextoEditable.Click
If Me.txtConAlgunValor.Text <> "" Then
Me.generarArchivoEditable()
End If
End Sub

</code>

Ahora, la funcion, en ella creo un RichTextBox, imprimo un par de lineas dandole algo de color, depues una funcion me devuelve un array de lineas , todo esto lo voy poniendo en el rtf.

Luego averiguo si existe el archivo de imagen, el cual busco en una ruta q construyo usando constantes y variables. Lo busco con y sin extension solo por si acaso.

entonces si existe llamo a otra funcion corta q lo incluye como bitmap (q es lo que se quiere), puedo seguir agregando texto, y finalmente selecciono todo el contenido del rtf, lo copio y lo pego al RTF del formulario que deseo.

Espero le sirva a quien necesite algo asi.

<code>

Private Sub generarArchivoEditable()

Dim rtb As System.Windows.Forms.RichTextBox
rtb = New RichTextBox

'Dim oSW As New StreamWriter("C:archivo_prueba.txt")
Dim Linea As String = "Línea de texto " & vbNewLine & "Otra linea de texto"

Dim lineas As New List(Of String)

cadena = " * * TITULO * * " & vbNewLine
Linea = cadena
'oSW.WriteLine(Linea)
rtb.AppendText(Linea)

'' RTF formato
rtb.Select(0, cadena.Length)
rtb.SelectionColor = Color.DarkBlue

cadena = " --- Datos --- " & vbNewLine

lineas = Desk_leerDatos(Me.txtAlgunValor.Text)

For Each miLinea As String In lineas
cadena += miLinea & vbNewLine
Next

Linea = cadena
'oSW.WriteLine(Linea)
rtb.AppendText(Linea)

rtb.AppendText(vbNewLine)
'' INTRODUZCO LA IMAGEN
Dim rutamapa As String = System.IO.Path.Combine(RutaDESK2, "mapas")
rutamapa = System.IO.Path.Combine(rutamapa, MAPASultimaimagen)
If IO.File.Exists(rutamapa) Then
InsertImage(rtb, rutamapa)
ElseIf IO.File.Exists(rutamapa & ".bmp") Then
InsertImage(rtb, rutamapa & ".bmp")
Else
MsgBox("No se encontro el archivo " & rutamapa)
End If
''
rtb.AppendText(vbNewLine)

....

rtb.SelectAll()
rtb.Copy()

' traigo el editable
FormReporteTexto.RichTextBox1.Paste()

FormReporteTexto.MdiParent = MDIParent1
FormReporteTexto.Show()

MsgBox(" terminado ")

End Sub


Public Sub InsertImage(ByRef notebox As RichTextBox, ByVal s As String)

Dim lstrFile As String = s
Dim myBitmap As Bitmap = New Bitmap(lstrFile)
' Copy the bitmap to the clipboard.

Clipboard.SetDataObject(myBitmap)
' Get the format for the object type.

Dim myFormat As DataFormats.Format = DataFormats.GetFormat(DataFormats.Bitmap)
'After verifying that the data can be pasted, paste

If (notebox.CanPaste(myFormat)) Then
notebox.Paste(myFormat)

Else
MessageBox.Show("The data format that you attempted site" & _
" is not supportedby this control.")
End If
End Sub

</code>

Como pueden ver la funcion se hace con el portapapeles. Hay otras maneras, pero no tan sencillas. No recuerdo de donde saque el dato por eso no pongo referencias. El resto de codigo si lo hice yo y lo pongo como ejemplo de un documento que tenga tanto texto como imagen.

saludos