Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As Long) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
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
Private Const SRCCOPY = &HCC0020
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Const BLACK_PEN = 6
Private Const WHITE_BRUSH = 0
Private Const NULL_BRUSH = 5
Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" (ByVal H As Long, ByVal W As Long, ByVal E As Long, ByVal O As Long, ByVal W As Long, ByVal I As Long, ByVal u As Long, ByVal S As Long, ByVal C As Long, ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, ByVal PAF As Long, ByVal F As String) As Long
Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Const ANSI_CHARSET = 0
Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Private Declare Function GetBkMode Lib "gdi32" (ByVal hdc As Long) As Long
Private Const TRANSPARENT = 1
Public Function Txt2Image(ByVal Text As String) As StdPicture
Dim mem_dc As Long
Dim mem_bm As Long
Dim orig_bm As Long
Dim wid As Long
Dim hgt As Long
Dim old_font As Long
Dim new_font As Long
Dim old_bk_mode As Long
'GoTo Here
wid = 720
hgt = 200
' Create the device context.
mem_dc = CreateCompatibleDC(hdc)
' Create the bitmap.
mem_bm = CreateCompatibleBitmap(mem_dc, wid, hgt)
' Make the device context use the bitmap.
orig_bm = SelectObject(mem_dc, mem_bm)
' Give the device context a white background.
SelectObject mem_dc, GetStockObject(WHITE_BRUSH)
Rectangle mem_dc, 0, 0, wid, hgt
SelectObject mem_dc, GetStockObject(NULL_BRUSH)
' Draw the on the device context.
SelectObject mem_dc, GetStockObject(BLACK_PEN)
MoveToEx mem_dc, 0, 0, ByVal 0&
MoveToEx mem_dc, 0, hgt, ByVal 0&
' Do not fill the background.
old_bk_mode = GetBkMode(mem_dc)
SetBkMode mem_dc, TRANSPARENT
' Give the DC a font.
new_font = CreateFont(25, 0, 0, 0, _
700, 0, 0, 0, ANSI_CHARSET, _
0, 0, 0, 0, "Tahoma")
old_font = SelectObject(mem_dc, new_font)
' Draw some text.
TextOut mem_dc, 10, 20, Text, Len(Text)
' Destroy the new font.
SelectObject mem_dc, old_font
DeleteObject new_font
' Restore the original background fill mode.
SetBkMode mem_dc, old_bk_mode
ImportQuestion.Picture1.AutoRedraw = True
' Copy the device context into the PictureBox.
BitBlt ImportQuestion.Picture1.hdc, 0, 0, wid, hgt, _
mem_dc, 0, 0, SRCCOPY
ImportQuestion.Picture1.Picture = ImportQuestion.Picture1.Image
Here:
With ImportQuestion
'.ForeColor = vbGreen
'TextOut .hdc, 5, 10, Text, Len(Text)
Set Txt2Image = .Picture1.Picture
End With
' Delete the bitmap and dc.
SelectObject mem_dc, orig_bm
DeleteObject mem_bm
DeleteDC mem_dc
End Function