Public Sub ToImage(ByVal cntrl As RichTextBox, ByVal filename As String, ByVal inputformat As Imaging.PixelFormat, ByVal outputformat As Imaging.ImageFormat)
Dim charfrom As Integer, charto As Integer
' Mark starting and ending character
Dim cRange As CHARRANGE
cRange.cpMin = charFrom
cRange.cpMax = charTo
' Calculate the area to render and print
Dim rectToPrint As RECT
rectToPrint.Top = CInt(cntrl.Top * AnInch)
rectToPrint.Bottom = CInt(cntrl.Bottom * AnInch)
rectToPrint.Left = CInt(cntrl.Left * AnInch)
rectToPrint.Right = CInt(cntrl.Right * AnInch)
Dim newImage As New Bitmap(cntrl.ClientRectangle.Width, cntrl.ClientRectangle.Height + 20, inputformat)
' Dim newImage As New Bitmap(cntrl.ClientRectangle.Width, cntrl.ClientRectangle.Height + 20, inputformat)
Dim gr As Graphics = Graphics.FromImage(newImage)
Using gr
gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
gr.Clear(Color.Transparent)
Dim hdc As IntPtr = gr.GetHdc()
Dim fmtRange As FORMATRANGE
fmtRange.chrg = cRange ' Indicate character from to character to
fmtRange.hdc = hdc ' Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc ' Point at printer hDC
fmtRange.rc = rectToPrint ' Indicate the area on page to print
Dim res As IntPtr = IntPtr.Zero
Dim wparam As IntPtr = IntPtr.Zero
wparam = New IntPtr(1)
' Move the pointer to the FORMATRANGE structure in memory
Dim lparam As IntPtr = IntPtr.Zero
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
Marshal.StructureToPtr(fmtRange, lparam, False)
' Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam)
' Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam)
' Release the device context handle obtained by a previous call
res = SendMessage(Handle, EM_FORMATRANGE, wparam, New IntPtr(0))
gr.ReleaseHdc(hdc)
End Using
newImage.Save(filename, outputformat)
newImage.Dispose()
End Sub
Dim charfrom As Integer, charto As Integer
' Mark starting and ending character
Dim cRange As CHARRANGE
cRange.cpMin = charFrom
cRange.cpMax = charTo
' Calculate the area to render and print
Dim rectToPrint As RECT
rectToPrint.Top = CInt(cntrl.Top * AnInch)
rectToPrint.Bottom = CInt(cntrl.Bottom * AnInch)
rectToPrint.Left = CInt(cntrl.Left * AnInch)
rectToPrint.Right = CInt(cntrl.Right * AnInch)
Dim newImage As New Bitmap(cntrl.ClientRectangle.Width, cntrl.ClientRectangle.Height + 20, inputformat)
' Dim newImage As New Bitmap(cntrl.ClientRectangle.Width, cntrl.ClientRectangle.Height + 20, inputformat)
Dim gr As Graphics = Graphics.FromImage(newImage)
Using gr
gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
gr.Clear(Color.Transparent)
Dim hdc As IntPtr = gr.GetHdc()
Dim fmtRange As FORMATRANGE
fmtRange.chrg = cRange ' Indicate character from to character to
fmtRange.hdc = hdc ' Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc ' Point at printer hDC
fmtRange.rc = rectToPrint ' Indicate the area on page to print
Dim res As IntPtr = IntPtr.Zero
Dim wparam As IntPtr = IntPtr.Zero
wparam = New IntPtr(1)
' Move the pointer to the FORMATRANGE structure in memory
Dim lparam As IntPtr = IntPtr.Zero
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
Marshal.StructureToPtr(fmtRange, lparam, False)
' Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam)
' Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam)
' Release the device context handle obtained by a previous call
res = SendMessage(Handle, EM_FORMATRANGE, wparam, New IntPtr(0))
gr.ReleaseHdc(hdc)
End Using
newImage.Save(filename, outputformat)
newImage.Dispose()
End Sub