Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the following borrowed code to insert an image into an RTF, as that aspect of RTF seemed way beyond me

Public Sub InsertImage(ByVal _image As Image)
        Dim _rtf As New StringBuilder()
        ' Append the RTF header
        _rtf.Append(RTF_HEADER)
        ' Create the font table using the RichTextBox's current font and append
        ' it to the RTF string
        _rtf.Append(GetFontTable(Me.Font))
        ' Create the image control string and append it to the RTF string
        _rtf.Append(GetImagePrefix(_image))
        ' Create the Windows Metafile and append its bytes in HEX format
        _rtf.Append(GetRtfImage(_image))
        ' Close the RTF image control string
        _rtf.Append(RTF_IMAGE_POST)
        Me.SelectedRtf = _rtf.ToString()
    End Sub
Private Function GetImagePrefix(ByVal _image As Image) As String
        Dim _rtf As New StringBuilder()
        ' Calculate the current width of the image in (0.01)mm
        Dim picw As Integer = CType(Math.Round((_image.Width / xDpi) * HMM_PER_INCH), Integer)
        ' Calculate the current height of the image in (0.01)mm
        Dim pich As Integer = CType(Math.Round((_image.Height / yDpi) * HMM_PER_INCH), Integer)
        ' Calculate the target width of the image in twips
        Dim picwgoal As Integer = CType(Math.Round((_image.Width / xDpi) * TWIPS_PER_INCH), Integer)
        ' Calculate the target height of the image in twips
        Dim pichgoal As Integer = CType(Math.Round((_image.Height / yDpi) * TWIPS_PER_INCH), Integer)
        
        ' Append values to RTF string
        _rtf.Append("{\pict\wmetafile8")
        _rtf.Append("\picw")
        _rtf.Append(picw)
        _rtf.Append("\pich")
        _rtf.Append(pich)
        _rtf.Append("\picwgoal")
        _rtf.Append(picwgoal)
        _rtf.Append("\pichgoal")
        _rtf.Append(picw)
        _rtf.Append(" ")
        Return _rtf.ToString()
    End Function
<DllImportAttribute("gdiplus.dll")> _
    Private Shared Function GdipEmfToWmfBits(ByVal _hEmf As IntPtr, ByVal _bufferSize As UInt32, ByVal _buffer As Byte(), ByVal _mappingMode As Integer, ByVal _flags As EmfToWmfBitsFlags) As UInt32
    End Function
    ''' <summary>
    ''' Wraps the image in an Enhanced Metafile by drawing the image onto the
    ''' graphics context, then converts the Enhanced Metafile to a Windows
    ''' Metafile, and finally appends the bits of the Windows Metafile in HEX
    ''' to a string and returns the string.
    ''' </summary>
    ''' <param name="_image"></param>
    ''' <returns>
    ''' A string containing the bits of a Windows Metafile in HEX
    ''' </returns>
    Private Function GetRtfImage(ByVal _image As Image) As String
        Dim _rtf As StringBuilder = Nothing
        ' Used to store the enhanced metafile
        Dim _stream As MemoryStream = Nothing
        ' Used to create the metafile and draw the image
        Dim _graphics As Graphics = Nothing
        ' The enhanced metafile
        Dim _metaFile As Metafile = Nothing
        ' Handle to the device context used to create the metafile
        Dim _hdc As IntPtr
        Try
            _rtf = New StringBuilder()
            _stream = New MemoryStream()
            ' Get a graphics context from the RichTextBox
            Using _graphics1 As Graphics = Me.CreateGraphics()
                ' Get the device context from the graphics context
                _hdc = _graphics1.GetHdc()
                ' Create a new Enhanced Metafile from the device context
                _metaFile = New Metafile(_stream, _hdc)
                ' Release the device context
                _graphics1.ReleaseHdc(_hdc)
            End Using
            ' Get a graphics context from the Enhanced Metafile
            Using _graphics1 As Graphics = Graphics.FromImage(_metaFile)
                ' Draw the image on the Enhanced Metafile
                _graphics1.DrawImage(_image, New Rectangle(0, 0, _image.Width, _image.Height))
            End Using
            ' Get the handle of the Enhanced Metafile
            Dim _hEmf As IntPtr = _metaFile.GetHenhmetafile()
            ' A call to EmfToWmfBits with a null buffer return the size of the
            ' buffer need to store the WMF bits.  Use this to get the buffer
            ' size.
            Dim _bufferSize As UInt32 = GdipEmfToWmfBits(_hEmf, 0, Nothing, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)
            ' Create an array to hold the bits
            Dim _buffer As Byte() = New Byte(CInt(_bufferSize)) {}
            ' A call to EmfToWmfBits with a valid buffer copies the bits into the
            ' buffer an returns the number of bits in the WMF.  
            Dim _convertedSize As UInt32 = GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)
            For i As Integer = 0 To _buffer.Length - 1
                ' Append the bits to the RTF string
                _rtf.Append([String].Format("{0:X2}", _buffer(i)))
            Next
            Return _rtf.ToString()
        Finally
            If _graphics IsNot Nothing Then
                _graphics.Dispose()
            End If
            If _metaFile IsNot Nothing Then
                _metaFile.Dispose()
            End If
            If _stream IsNot Nothing Then
                _stream.Close()
            End If
        End Try
    End Function


sorry for so much code, anyway if i use that, the image is huge, here is a sample untouched image http://img339.imageshack.us/img339/7889/barcode128g4.png[^]
i tried inserting the image with wordpad and copying the picw, pich, pichgoal, and picwgoal values from the wordpad generated RTF, but then the image looked almost squeezed together. i did notice the image hex values seemed quite different, i can't simply copy as the image itself will be different each run, I am at a loss, any help is appreciated
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900