[COLOR=black][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] saveAsImage([COLOR=blue]ByVal[/COLOR] box [COLOR=blue]As[/COLOR] RichTextBox, _
[COLOR=blue]ByVal[/COLOR] filename [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], [COLOR=blue]ByVal[/COLOR] format [COLOR=blue]As[/COLOR] Imaging.ImageFormat)
[COLOR=blue]Dim[/COLOR] rect [COLOR=blue]As[/COLOR] Rectangle = box.DisplayRectangle
[COLOR=blue]Dim[/COLOR] bmp [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] Bitmap(rect.Width, rect.Height, Imaging.PixelFormat.Format32bppArgb)
[COLOR=blue]Dim[/COLOR] g [COLOR=blue]As[/COLOR] Graphics = Graphics.FromImage(bmp)
[COLOR=blue]Dim[/COLOR] p [COLOR=blue]As[/COLOR] Point
g.CopyFromScreen(box.PointToScreen(p), p, [COLOR=blue]New[/COLOR] Size(rect.Width, rect.Height))
g.Dispose()
bmp.Save(filename, format)
[SIZE=2] bmp.Dispose()
[/SIZE][COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
[/COLOR]
[COLOR=black]saveAsImage(RichTextBox1, [COLOR=maroon]"rich.bmp"[/COLOR], Imaging.ImageFormat.Bmp)
[/COLOR]
[COLOR=black]<DllImport([COLOR=maroon]"gdi32.dll"[/COLOR], CharSet:=CharSet.Auto, SetLastError:=[COLOR=blue]True[/COLOR], ExactSpelling:=[COLOR=blue]True[/COLOR])> _
[COLOR=blue]Public[/COLOR] [COLOR=blue]Shared[/COLOR] [COLOR=blue]Function[/COLOR] BitBlt([COLOR=blue]ByVal[/COLOR] hDC [COLOR=blue]As[/COLOR] IntPtr, [COLOR=blue]ByVal[/COLOR] x [COLOR=blue]As[/COLOR] Int32, [COLOR=blue]ByVal[/COLOR] y [COLOR=blue]As[/COLOR] Int32, _
[COLOR=blue]ByVal[/COLOR] nWidth [COLOR=blue]As[/COLOR] Int32, [COLOR=blue]ByVal[/COLOR] nHeight [COLOR=blue]As[/COLOR] Int32, _
[COLOR=blue]ByVal[/COLOR] hSrcDC [COLOR=blue]As[/COLOR] IntPtr, [COLOR=blue]ByVal[/COLOR] xSrc [COLOR=blue]As[/COLOR] Int32, [COLOR=blue]ByVal[/COLOR] ySrc [COLOR=blue]As[/COLOR] Int32, [COLOR=blue]ByVal[/COLOR] dwRop [COLOR=blue]As[/COLOR] Int32) [COLOR=blue]As[/COLOR] Int32
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
[COLOR=blue][COLOR=blue]Public[/COLOR] [COLOR=blue]Shared[/COLOR] [COLOR=blue]Sub[/COLOR] CopyFromScreen([COLOR=blue]ByVal[/COLOR] g [COLOR=blue]As[/COLOR] Graphics, [COLOR=blue]ByVal[/COLOR] sourceX [COLOR=blue]As[/COLOR] Int32, [COLOR=blue]ByVal[/COLOR] sourceY [COLOR=blue]As[/COLOR] Int32, _
[COLOR=black][COLOR=blue]ByVal[/COLOR] destX [COLOR=blue]As[/COLOR] Int32, [COLOR=blue]ByVal[/COLOR] destY [COLOR=blue]As[/COLOR] Int32, [COLOR=blue]ByVal[/COLOR] blockRegionSize [COLOR=blue]As[/COLOR] Size)
[COLOR=blue]Const[/COLOR] SourceCopy [COLOR=blue]As[/COLOR] Int32 = 13369376
[COLOR=blue]Dim[/COLOR] width [COLOR=blue]As[/COLOR] Int32 = blockRegionSize.Width
[COLOR=blue]Dim[/COLOR] height [COLOR=blue]As[/COLOR] Int32 = blockRegionSize.Height
[COLOR=blue]Dim[/COLOR] screen [COLOR=blue]As[/COLOR] Graphics = Graphics.FromHwnd(IntPtr.Zero)
[COLOR=blue]Dim[/COLOR] source [COLOR=blue]As[/COLOR] IntPtr = screen.GetHdc
[COLOR=blue]Dim[/COLOR] dest [COLOR=blue]As[/COLOR] IntPtr = g.GetHdc
[COLOR=blue]Try[/COLOR]
BitBlt(dest, destX, destY, width, height, source, sourceX, sourceY, SourceCopy)
[COLOR=blue]Finally[/COLOR]
g.ReleaseHdc()
screen.ReleaseHdc() [COLOR=green]'not needed?[/COLOR]
screen.Dispose()
[COLOR=blue] End[/COLOR] [COLOR=blue]Try[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
[/COLOR][/COLOR][/COLOR][COLOR=black][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] saveAsImage([COLOR=blue]ByVal[/COLOR] box [COLOR=blue]As[/COLOR] RichTextBox, [COLOR=blue]ByVal[/COLOR] filename [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], [COLOR=blue]ByVal[/COLOR] format [COLOR=blue]As[/COLOR] Imaging.ImageFormat)
[COLOR=blue] Dim[/COLOR] rect [COLOR=blue]As[/COLOR] Rectangle = box.DisplayRectangle
[COLOR=blue]Dim[/COLOR] bmp [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] Bitmap(rect.Width, rect.Height, Imaging.PixelFormat.Format32bppArgb)
[COLOR=blue]Dim[/COLOR] g [COLOR=blue]As[/COLOR] Graphics = Graphics.FromImage(bmp)
[COLOR=blue]Dim[/COLOR] p [COLOR=blue]As[/COLOR] Point = box.PointToScreen([COLOR=blue]New[/COLOR] Point(0, 0))
[COLOR=blue]Try[/COLOR]
CopyFromScreen(g, p.X, p.Y, 0, 0, bmp.Size)
bmp.Save(filename, format)
[COLOR=blue]Finally[/COLOR]
g.Dispose()
bmp.Dispose()
[COLOR=blue]End[/COLOR] [COLOR=blue]Try[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
[/COLOR]
MSDN2 said:Graphics.ReleaseHdc Method ()
Note: This method is new in the .NET Framework version 2.0.
[SIZE=2]g.ReleaseHdc()
screen.ReleaseHdc() [/SIZE][SIZE=2][COLOR=#008000]'not needed?
[/COLOR][/SIZE][SIZE=2]screen.Dispose()
[/SIZE]
g.ReleaseHdc(dest)
screen.ReleaseHdc(source) [SIZE=2][COLOR=#008000]'not needed?[/COLOR][/SIZE]
screen.Dispose()
Option Explicit On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Drawing.Printing
Namespace RichTextBoxPrintCtrl
Public Class RichTextBoxPrintCtrl
Inherits RichTextBox
' Convert the unit that is used by the .NET framework (1/100 inch)
' and the unit that is used by Win32 API calls (twips 1/1440 inch)
Private Const AnInch As Double = 14.4
<StructLayout(LayoutKind.Sequential)> _
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure CHARRANGE
Public cpMin As Integer ' First character of range (0 for start of doc)
Public cpMax As Integer ' Last character of range (-1 for end of doc)
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure FORMATRANGE
Public hdc As IntPtr ' Actual DC to draw on
Public hdcTarget As IntPtr ' Target DC for determining text formatting
Public rc As Rect ' Region of the DC to draw to (in twips)
Public rcPage As Rect ' Region of the whole DC (page size) (in twips)
Public chrg As CHARRANGE ' Range of text to draw (see above declaration)
End Structure
Private Const WM_USER As Integer = &H400
Private Const EM_FORMATRANGE As Integer = WM_USER + 57
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
' Render the contents of the RichTextBox for printing
' Return the last character printed + 1 (printing start from this point for next page)
Public Function Print(ByVal charFrom As Integer, ByVal charTo As Integer, ByVal e As PrintPageEventArgs) 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 = e.MarginBounds.Top * AnInch
rectToPrint.Bottom = e.MarginBounds.Bottom * AnInch
rectToPrint.Left = e.MarginBounds.Left * AnInch
rectToPrint.Right = e.MarginBounds.Right * AnInch
' Calculate the size of the page
Dim rectPage As RECT
rectPage.Top = e.PageBounds.Top * AnInch
rectPage.Bottom = e.PageBounds.Bottom * AnInch
rectPage.Left = e.PageBounds.Left * AnInch
rectPage.Right = e.PageBounds.Right * AnInch
Dim hdc As IntPtr = e.Graphics.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
fmtRange.rcPage = rectPage ' Indicate whole size of page
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
e.Graphics.ReleaseHdc(hdc)
' Return last + 1 character printer
Return res.ToInt32()
End Function
End Class
End Namespace
What if you bind to SendMessageW (the WideString version), not SendMessageA (the AnsisString version)?VB.NET:Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA"
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] mnuPrint_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] mnuPrint.Click
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] PrintDialog1.ShowDialog() = DialogResult.OK [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]PrintDocument1.Print()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#008000]' Menu Print click
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] checkPrint [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PrintDocument1_BeginPrint([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Drawing.Printing.PrintEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PrintDocument1.BeginPrint
checkPrint = 0
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] MnuPrintPreview_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] MnuPrintPreview.Click
PrintPreviewDialog1.ShowDialog()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PrintDocument1_PrintPage([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Drawing.Printing.PrintPageEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PrintDocument1.PrintPage
[/SIZE][SIZE=2][COLOR=#008000]' Print the content of the RichTextBox. Store the last character printed.
[/COLOR][/SIZE][SIZE=2]checkPrint = CurForm.RichTextBox1.Print(checkPrint, CurForm.RichTextBox1.TextLength, e)
[/SIZE][SIZE=2][COLOR=#008000]' Look for more pages
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] checkPrint < CurForm.RichTextBox1.TextLength [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]e.HasMorePages = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]e.HasMorePages = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]