[RESOLVED] Text Rich Text Box not showing when saving panel content as Image
Hello,
Currently, I am using the code below to save the contents on a panel as an image. The thing is, all contents except for the text in the rich textbox is being saved. Can anyone help me out?
Declare Auto Function SendMessage Lib "user32" ( _
ByVal hWnd As IntPtr, _
ByVal Msg As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As Integer) As Integer
Private Enum EDrawingOptions As Integer
PRF_CHECKVISIBLE = &H1
PRF_NONCLIENT = &H2
PRF_CLIENT = &H4
PRF_ERASEBKGND = &H8
PRF_CHILDREN = &H10
PRF_OWNED = &H20
End Enum
Private Function PrintPanel()
Const WM_PRINT As Integer = &H317
Dim myBmp As Bitmap
Dim myGraphics As Graphics
Dim hdc As System.IntPtr
myBmp = New Bitmap( _
Me.pnlContent.DisplayRectangle.Width + pnlContent.Padding.Left + pnlContent.Padding.Right, _
Me.pnlContent.DisplayRectangle.Height + pnlContent.Padding.Top + pnlContent.Padding.Bottom)
myGraphics = Graphics.FromImage(myBmp)
myGraphics.DrawRectangle(Pens.White, New Rectangle(0, 0,
Me.pnlContent.DisplayRectangle.Width + pnlContent.Padding.Left + pnlContent.Padding.Right * 2, Me.pnlContent.DisplayRectangle.Height + pnlContent.Padding.Top + pnlContent.Padding.Bottom * 2))
hdc = myGraphics.GetHdc
'"FormsDispPanel" is your PAnel to print
Call SendMessage(pnlContent.Handle, WM_PRINT, hdc, _
EDrawingOptions.PRF_CHILDREN Or _
EDrawingOptions.PRF_CLIENT Or _
EDrawingOptions.PRF_NONCLIENT Or _
EDrawingOptions.PRF_OWNED)
myGraphics.ReleaseHdc(hdc)
myBmp.Save(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\out.png")
myGraphics.Dispose()
myGraphics = Nothing
myBmp = Nothing
Return True
End Function
Friend WithEvents prntDoc As New PrintDocument()
Private Sub prntDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles prntDoc.PrintPage
e.Graphics.DrawImage(Me.PictureBox1.Image, 0, 0)
End Sub
Thanks in Advance!
Re: Text Rich Text Box not showing when saving panel content as Image
Quote:
Originally Posted by
OmariCelestine
Currently, I am using the code below to save the contents on a panel as an image. The thing is, all contents except for the text in the rich textbox is being saved. Can anyone help me out?
I was going to suggest using DrawToBitmap but after testing I noticed the text in RichTextBox isn't captured with that either!
If the panel control isn't going to be covered by other windows maybe try CopyFromScreen, something like...
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim b As Bitmap = CaptureControl(Panel1) ' capture panel area
b.Save("M:\CapturePanel.png", Imaging.ImageFormat.Png) ' save to file
PictureBox1.Image = b ' display in pic box
End Sub
Private Shared Function CaptureControl(ByVal control As Control) As Bitmap
Dim bmp As Bitmap = New Bitmap(control.ClientSize.Width, control.ClientSize.Height)
Using gr As Graphics = Graphics.FromImage(bmp)
gr.CopyFromScreen(control.PointToScreen(control.ClientRectangle.Location), Point.Empty, control.ClientSize)
End Using
Return bmp
End Function
End Class
Re: Text Rich Text Box not showing when saving panel content as Image
I haven't done any work on it so treat this merely as a crazy idea but might it be possible to use the RichTextBoxPrintCtrl, directing output to a bitmap rather than a print document?
Re: Text Rich Text Box not showing when saving panel content as Image
Quote:
Originally Posted by
Edgemeal
I was going to suggest using DrawToBitmap but after testing I noticed the text in RichTextBox isn't captured with that either!
If the panel control isn't going to be covered by other windows maybe try CopyFromScreen, something like...
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim b As Bitmap = CaptureControl(Panel1) ' capture panel area
b.Save("M:\CapturePanel.png", Imaging.ImageFormat.Png) ' save to file
PictureBox1.Image = b ' display in pic box
End Sub
Private Shared Function CaptureControl(ByVal control As Control) As Bitmap
Dim bmp As Bitmap = New Bitmap(control.ClientSize.Width, control.ClientSize.Height)
Using gr As Graphics = Graphics.FromImage(bmp)
gr.CopyFromScreen(control.PointToScreen(control.ClientRectangle.Location), Point.Empty, control.ClientSize)
End Using
Return bmp
End Function
End Class
The code works but one of my panels are scrollable in which it only saves an image of the visible area of the panel.
Re: Text Rich Text Box not showing when saving panel content as Image
Quote:
Originally Posted by
OmariCelestine
The code works but one of my panels are scrollable in which it only saves an image of the visible area of the panel.
Never did it but some ideas,
Draw every control using DrawToBitmap to a bitmap, but then you'd still have the same problem with the RichTextBox control.
Or use CopyFromScreen like above but temporarily resize the scrollable control so everything inside it is viewable (painted), but that won't work if the control is large/off the screen, so instead of CopyFromScreen maybe try using the PrintWindow API since it can capture windows behind other windows and off to the side of the desktop area....
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim b As Bitmap = Capture_Window(Panel1)
b.Save("M:\Cap_window.png", Imaging.ImageFormat.Png) ' save to png type file
PictureBox1.Image = b
End Sub
Private Declare Function PrintWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal hDC As IntPtr, ByVal nFlags As UInteger) As Boolean
Public Function Capture_Window(ByVal control As Control) As Bitmap
Dim bmp As New Bitmap(control.Width, control.Height)
Using g As Graphics = Graphics.FromImage(bmp)
Dim hDC As IntPtr = g.GetHdc()
PrintWindow(control.Handle, hDC, 0)
g.ReleaseHdc(hDC)
End Using
Return bmp
End Function
EDIT: That won't work, where panel is larger then form it will be black, you'd have to set the panel as child of the desktop then capture it, what a hassle! Sorry!
Re: Text Rich Text Box not showing when saving panel content as Image
Quote:
Originally Posted by
Edgemeal
Never did it but some ideas,
Draw every control using DrawToBitmap to a bitmap, but then you'd still have the same problem with the RichTextBox control.
Or use CopyFromScreen like above but temporarily resize the scrollable control so everything inside it is viewable (painted), but that won't work if the control is large/off the screen, so instead of CopyFromScreen maybe try using the PrintWindow API since it can capture windows behind other windows and off to the side of the desktop area....
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim b As Bitmap = Capture_Window(Panel1)
b.Save("M:\Cap_window.png", Imaging.ImageFormat.Png) ' save to png type file
PictureBox1.Image = b
End Sub
Private Declare Function PrintWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal hDC As IntPtr, ByVal nFlags As UInteger) As Boolean
Public Function Capture_Window(ByVal control As Control) As Bitmap
Dim bmp As New Bitmap(control.Width, control.Height)
Using g As Graphics = Graphics.FromImage(bmp)
Dim hDC As IntPtr = g.GetHdc()
PrintWindow(control.Handle, hDC, 0)
g.ReleaseHdc(hDC)
End Using
Return bmp
End Function
EDIT: That won't work, where panel is larger then form it will be black, you'd have to set the panel as child of the desktop then capture it, what a hassle! Sorry!
In this case, it captures the panel with the scrollbars but does not print under the scrollable areas.
Re: [RESOLVED] Text Rich Text Box not showing when saving panel content as Image
I got the issue resolved by combining a function I found elsewhere on the internet with the one I was using. Finally! Thank you all for your support. :)