Results 1 to 6 of 6

Thread: Picture In Rich Text Box?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    28

    Picture In Rich Text Box?

    You know how in microsoft word you can insert a picture into your document, is that possible in a rich text box? If so, how would you go about it, thanks for help.
    Last edited by Hepy; Apr 11th, 2004 at 06:36 AM.

  2. #2
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236
    He is a little app that pastes a picture into a RichtextBox. I am sure it can be adapted. I didn't make the app.
    Attached Files Attached Files

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    28

    Thanks

    Thanks that just what i wanted

  4. #4
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type Size
    4.     cx As Long
    5.     cy As Long
    6. End Type
    7.  
    8. Private Type POINTAPI
    9.     x As Long
    10.     y As Long
    11. End Type
    12.  
    13. Private Type BITMAP
    14.     bmType As Long
    15.     bmWidth As Long
    16.     bmHeight As Long
    17.     bmWidthBytes As Long
    18.     bmPlanes As Integer
    19.     bmBitsPixel As Integer
    20.     bmBits As Long
    21. End Type
    22.  
    23. 'Private Type METAHEADER
    24. '    mtType As Integer
    25. '    mtHeaderSize As Integer
    26. '    mtVersion As Integer
    27. '    mtSize As Long
    28. '    mtNoObjects As Integer
    29. '    mtMaxRecord As Long
    30. '    mtNoParameters As Integer
    31. 'End Type
    32.  
    33. ' Used to create the metafile
    34. Private Declare Function CreateMetaFile Lib "gdi32" Alias "CreateMetaFileA" (ByVal lpString As String) As Long
    35. Private Declare Function CloseMetaFile Lib "gdi32" (ByVal hDCMF As Long) As Long
    36. Private Declare Function DeleteMetaFile Lib "gdi32" (ByVal hMF As Long) As Long
    37. ' 6 APIs used to render/embed the bitmap in the metafile
    38. Private Declare Function SetMapMode Lib "gdi32" (ByVal hdc As Long, ByVal nMapMode As Long) As Long
    39. Private Declare Function SetWindowExtEx Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpSize As Size) As Long
    40. Private Declare Function SetWindowOrgEx Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpPoint As POINTAPI) As Long
    41. Private Declare Function SaveDC Lib "gdi32" (ByVal hdc As Long) As Long
    42. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    43. Private Declare Function RestoreDC Lib "gdi32" (ByVal hdc As Long, ByVal nSavedDC As Long) As Long
    44. ' These APIs are used to BitBlt the bitmap image into the metafile
    45. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    46. Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    47. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    48. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    49. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    50. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    51. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    52.  
    53. ' Used for creating the temporary WMF file
    54. Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    55.  
    56. Private Const MM_ANISOTROPIC = 8 ' Map mode anisotropic
    57. Public Function StdPicAsRTF(aStdPic As StdPicture) As String
    58.     Dim hMetaDC     As Long
    59.     Dim hMeta       As Long
    60.     Dim hPicDC      As Long
    61.     Dim hOldBmp     As Long
    62.     Dim aBMP        As BITMAP
    63.     Dim aSize       As Size
    64.     Dim aPt         As POINTAPI
    65.     Dim fileName    As String
    66. '    Dim aMetaHdr    As METAHEADER
    67.     Dim screenDC    As Long
    68.     Dim headerStr   As String
    69.     Dim retStr      As String
    70.     Dim byteStr     As String
    71.     Dim bytes()     As Byte
    72.     Dim filenum     As Integer
    73.     Dim numBytes    As Long
    74.     Dim i           As Long
    75.     ' Create a metafile to a temporary file in the registered windows TEMP folder
    76.     fileName = getTempName("WMF")
    77.     hMetaDC = CreateMetaFile(fileName)
    78.     ' Set the map mode to MM_ANISOTROPIC
    79.     SetMapMode hMetaDC, MM_ANISOTROPIC
    80.     ' Set the metafile origin as 0, 0
    81.     SetWindowOrgEx hMetaDC, 0, 0, aPt
    82.     ' Get the bitmap's dimensions
    83.     GetObject aStdPic.Handle, Len(aBMP), aBMP
    84.     ' Set the metafile width and height
    85.     SetWindowExtEx hMetaDC, aBMP.bmWidth, aBMP.bmHeight, aSize
    86.     ' save the new dimensions
    87.     SaveDC hMetaDC
    88.     ' OK. Now transfer the freakin image to the metafile
    89.     screenDC = GetDC(0)
    90.     hPicDC = CreateCompatibleDC(screenDC)
    91.     ReleaseDC 0, screenDC
    92.     hOldBmp = SelectObject(hPicDC, aStdPic.Handle)
    93.     BitBlt hMetaDC, 0, 0, aBMP.bmWidth, aBMP.bmHeight, hPicDC, 0, 0, vbSrcCopy
    94.     SelectObject hPicDC, hOldBmp
    95.     DeleteDC hPicDC
    96.     DeleteObject hOldBmp
    97.     ' "redraw" the metafile DC
    98.     RestoreDC hMetaDC, True
    99.     ' close it and get the metafile handle
    100.     hMeta = CloseMetaFile(hMetaDC)
    101.    
    102. '    GetObject hMeta, Len(aMetaHdr), aMetaHdr
    103.     ' delete it from memory
    104.     DeleteMetaFile hMeta
    105.    
    106.     ' Do the RTF header for the object. This little bit is sometimes required on
    107.     '  earlier versions of the rich text box and in certain operating systems
    108.     '  (WinNT springs to mind)
    109.     headerStr = "{\rtf1\ansi"
    110.     ' Picture specific tag stuff
    111.     headerStr = headerStr & _
    112.                 "{\pict\picscalex100\picscaley100" & _
    113.                 "\picw" & aStdPic.Width & "\pich" & aStdPic.Height & _
    114.                 "\picwgoal" & aBMP.bmWidth * Screen.TwipsPerPixelX & _
    115.                 "\pichgoal" & aBMP.bmHeight * Screen.TwipsPerPixelY & _
    116.                 "\wmetafile8"
    117.    
    118.     ' Get the size of the metafile
    119.     numBytes = FileLen(fileName)
    120.     ' Create our byte buffer for reading
    121.     ReDim bytes(1 To numBytes)
    122.     ' get a free file number
    123.     filenum = FreeFile()
    124.     ' open the file for input
    125.     Open fileName For Binary Access Read As #filenum
    126.     ' read the bytes
    127.     Get #filenum, , bytes
    128.     ' close the file
    129.     Close #filenum
    130.     ' Generate our hex encoded byte string
    131.     byteStr = String(numBytes * 2, "0")
    132.     For i = LBound(bytes) To UBound(bytes)
    133.         If bytes(i) > &HF Then
    134.             Mid$(byteStr, 1 + (i - 1) * 2, 2) = Hex$(bytes(i))
    135.         Else
    136.             Mid$(byteStr, 2 + (i - 1) * 2, 1) = Hex$(bytes(i))
    137.         End If
    138.     Next i
    139.     ' stick it all together
    140.     retStr = headerStr & " " & byteStr & "}"
    141.     ' Add in the closing RTF bit
    142.     retStr = retStr & "}"
    143.        
    144.     StdPicAsRTF = retStr
    145.     On Local Error Resume Next
    146.     ' Kill the temporary file
    147.     If Dir(fileName) <> "" Then Kill fileName
    148. End Function
    149. Private Function getTempName(Optional anExt As String = "tmp") As String
    150.     ' ***********************************************************************
    151.     '  Author: The Hand
    152.     '    Date: June, 2002
    153.     ' Company: EliteVB
    154.     '
    155.     '  Function: getTempName
    156.     ' Arguments: anExt - an extension to be used for the temp file. If none
    157.     '                    is provided, the function automatically uses "tmp"
    158.     '                    as the extension. It is up to the procedure that
    159.     '                    uses this temporary name to clean up the file (kill
    160.     '                    it) after it is created.
    161.     '
    162.     ' Description:
    163.     '    Creates a temporary filename in the registered system temp directory
    164.     ' ***********************************************************************
    165.     Dim tempPath    As String
    166.     Dim fileName    As String
    167.     Dim i           As Long
    168.    
    169.     Const validChars As String = "123567890qwertyuiopasdfghjklzxcvbnm"
    170.    
    171.     ' Create a buffer
    172.     tempPath = String$(255, " ")
    173.     ' get the system path
    174.     GetTempPath 255, tempPath
    175.     ' trim off the fat
    176.     tempPath = Left$(tempPath, InStr(tempPath, Chr$(0)) - 1)
    177.     ' Create a buffer
    178.     fileName = Space(12)
    179.     ' Put the non-random stuff into the string
    180.     Mid$(fileName, 1, 1) = "T"
    181.     Mid$(fileName, Len(fileName) - Len(anExt), 1) = "."
    182.     ' Add in the specified extension, if provided ("tmp" is default)
    183.     Mid$(fileName, Len(fileName) - Len(anExt) + 1, Len(anExt)) = anExt
    184.     ' fill the buffer with random stuff
    185.     Randomize
    186.     For i = 2 To Len(fileName) - 4
    187.         Mid$(fileName, i, 1) = Mid$(validChars, CLng(Rnd() * (Len(validChars)) + 1), 1)
    188.     Next i
    189.     tempPath = tempPath & fileName
    190.     ' return the path name
    191.     getTempName = tempPath
    192.    
    193. End Function
    194.  
    195.  
    196.  
    197.  
    198. Private Sub Command1_Click ()
    199. Pic.Picture = LoadPicture("c:\pic.bmp")
    200. Dim aStr As String
    201. aStr = StdPicAsRTF(Pic.Picture)
    202. RichTextBox.SelRTF = aStr
    203. End Sub



    I think this will help you!
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    28

    Thanks Again

    Another helpful piece of code, thanks

  6. #6
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315
    Thanks!













    Expert in RichText Box control!
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

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