Results 1 to 11 of 11

Thread: Solution for PrintForm in .NET

  1. #1

    Thread Starter
    New Member sokkmees's Avatar
    Join Date
    Sep 2002
    Location
    Estonia
    Posts
    11

    Cool Solution for PrintForm in .NET

    Hello!

    I just found a code snippet that is capable of getting control image even when the control is invisible or overlapped by others

    So forget about BitBlt problems

    Code:
        Private Function GetControlBitmap(ByVal CTL As Control) As Bitmap
            Dim Methods() As System.Reflection.MethodInfo = ctl.GetType.GetMethods( _
                System.Reflection.BindingFlags.Instance Or _
                System.Reflection.BindingFlags.NonPublic)
            Dim i As Integer
            Dim OnPaintMethod As System.Reflection.MethodInfo
    
            For i = 0 To Methods.Length - 1
                If Methods(i).Name = "OnPaint" Then
                    OnPaintMethod = Methods(i)
                    Exit For
                End If
            Next
    
            If Not OnPaintMethod Is Nothing Then
                Dim _BMP As New Bitmap(CTL.Width, CTL.Height)
                Dim _Gr As Graphics = Graphics.FromImage(_BMP)
                Dim PaintEventArgs As New System.Windows.Forms.PaintEventArgs(_Gr, _
                New Rectangle(0, 0, CTL.Width, CTL.Height))
                OnPaintMethod.Invoke(ctl, New Object() {PaintEventArgs})
                _Gr.Dispose()
                Return _BMP
            End If
        End Function

    Originally posted in MS VB.NET newsgroup by Craig Vick

  2. #2
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    What's the address to that newsgroup?
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  3. #3
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Pretty Cool

    Except, it can't do combo's and listview's and other controls of that nature.

    You wouldn't happen to have a fix for that?
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  4. #4

    Thread Starter
    New Member sokkmees's Avatar
    Join Date
    Sep 2002
    Location
    Estonia
    Posts
    11
    Address of the newsgroup (taken from Google) is
    microsoft.public.dotnet.languages.vb

    I'm afraid other controls still have to be drawn from code with simple drawstring, etc functions. At least at the moment I don't know the other way

    But this code helped me when I was stuck printing invisible control that does custom drawing under OnPaint event

  5. #5
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    Thanks.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  6. #6
    Junior Member
    Join Date
    Jun 2004
    Posts
    16
    ok, I'm new to vb but not programming, but i'm also an idiot.

    whenever i call this method from inside a PrintPage event and pass it the Form as the control it wants(casted or not), I use the bitmap it returns and it's always blank. Any idea why? or maybe I'm using it wrong.

    I used the BitBlt way, and it captures the GDI+ lines, etc. that I already have drawn on the form, but it also captures things on top of the form, like the stupid "generating preview" dialog box or sometimes taskbar.

    arrrgh, i just want to print whats on the form. this "screenshot" way, or variants of it, seem to be the only way to do it too.

  7. #7
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Angrycoder: You need to do the screenshot and save it in a variable before opening the printpreview.
    It sounds like you grab it in the PrintPage event or something.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  8. #8

    Thread Starter
    New Member sokkmees's Avatar
    Join Date
    Sep 2002
    Location
    Estonia
    Posts
    11
    Angrycoder: you have to pass every control on the form one-by-one. It then gives you bitmap that this control draws under its OnPaint event.

  9. #9
    Junior Member
    Join Date
    Jun 2004
    Posts
    16
    yeah, I have a form with no controls on it, only stuff drawn on it with GDI+. So I took pax's advice and used the BitBlt way and it works. Thanks guys.

  10. #10

    Thread Starter
    New Member sokkmees's Avatar
    Join Date
    Sep 2002
    Location
    Estonia
    Posts
    11
    Sure BitBlt is the easiest way if you have visible form

  11. #11
    Junior Member
    Join Date
    Jun 2004
    Posts
    16
    I thought this way would be better though, since I'm trying to correctly scale it to a sheet of paper (and handling all screen resolutions, bleh). even resizing the bitmap by interpolating and using antialiasing it still looks a bit grainy. but if I use this way and pass the form as the control i want to become a bitmap, it seems to ignore lines i've drawn on it, and returns a blank bitmap

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