Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on

PrintDocument problem

I have a PrintDocument control that is printing my Desktop instead of the form.
Here is an excerpt of the code:

      Dim nullObject As System.Object = 0
      Dim str As String = ""
      Dim nullObjStr As System.Object = str
      ....
      AxWebBrowser1.Navigate("http://www.MySite.com/Report.aspx?OrderID=" & spParam , nullObject, nullObjStr, nullObjStr, nullObjStr)
       CaptureScreen()
       PrintDocument1.PrinterSettings.PrinterName = _prtNameInkJ
       PrintDocument1.Print()
       ........

 Private Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As System.Int32) As Long
   Dim memoryImage As Bitmap

   Private Sub CaptureScreen()
      Dim mygraphics As Graphics = Me.CreateGraphics()
      Dim s As Size = Me.Size
      memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
      Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
      Dim dc1 As IntPtr = mygraphics.GetHdc
      Dim dc2 As IntPtr = memoryGraphics.GetHdc
      BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, dc1, 0, 0, 13369376)
      mygraphics.ReleaseHdc(dc1)
      memoryGraphics.ReleaseHdc(dc2)
   End Sub

   Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
      e.Graphics.DrawImage(memoryImage, 0, 0)
   End Sub

But it does show the www.MySite.com in the Web browser control.

Avatar of samtran0331
samtran0331
Flag of United States of America image

Mike,
Is your goal to print the web page?
Avatar of MikeMCSD

ASKER

YES!
If I do this:
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.Show()

it shows a screen shot of the current window, Visual Studio.
did you develop the web page? if so, you can add a button or link with javascript:window.print(); for the onclick event and it will print the page.
you could also screen scrape the web page...either method imho would be more efficient than getting a screenshot....
but, if you stick with the screenshot method....I wonder if it is because you are in debug mode that it returns the VS.net window?
like if you ran the final *.exe file it would give you your app's window instead of vs.net window...
ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks sam . . I tried using <body onload="window.print();">
but it seems to just ignore it.
Let me look at the rolla thing.

mistake .  . the  <body onload="window.print();">
works . . but only prints the last document in the run (I have a loop) . .
and the "Print" dialog appears (for the last document)