Results 1 to 7 of 7

Thread: [RESOLVED] Text Rich Text Box not showing when saving panel content as Image

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Location
    Grenada
    Posts
    12

    Resolved [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!

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Text Rich Text Box not showing when saving panel content as Image

    Quote Originally Posted by OmariCelestine View Post
    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
    Last edited by Edgemeal; Jul 14th, 2013 at 10:43 AM.

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Location
    Grenada
    Posts
    12

    Re: Text Rich Text Box not showing when saving panel content as Image

    Quote Originally Posted by Edgemeal View Post
    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.

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Text Rich Text Box not showing when saving panel content as Image

    Quote Originally Posted by OmariCelestine View Post
    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!
    Last edited by Edgemeal; Jul 14th, 2013 at 01:24 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Location
    Grenada
    Posts
    12

    Re: Text Rich Text Box not showing when saving panel content as Image

    Quote Originally Posted by Edgemeal View Post
    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Location
    Grenada
    Posts
    12

    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.

Tags for this Thread

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