Re: Printing graphics/text that span multiple pages
- From: "Bob Powell [MVP]" <bob@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 21 Nov 2006 11:46:25 +0100
the "handles" returned from Graphics.GetDC are not normal GDI handles
although they can be used as such. This just puts another layer of
win32ishness in and complicates the situation.
If you're dealing with managed drawing stay away from interop unless it
really needed for a performance issue or such.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Jeff" <jrush@xxxxxxx> wrote in message
news:1163697562.147987.239730@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I've always thought it was appropriate to pass handles rather than the
actual object, but you are correct --- if I pass the actual Graphics
rather than the hDC, I no longer receive the invalid handle error.
Thank you!
Regarding the matrix/transformation issue -- I am able to print
multiple pages now, but I still have a few issues I need to iron out.
Here is the new code inside the PrintPage event.
y = <starting Y position>
For Each ctrl In mControlList
Dim mx As Drawing2D.Matrix = New Drawing2D.Matrix(1, 0, 0, 1, 0,
-(mCount * 6000))
e.Graphics.Transform = mx
ctrl.RenderToDC(e.Graphics, y)
y = y + ctrl.ControlHeight
Next
mCount = mCount + 1
If mCount < 2 Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
1) If I use "y" instead of 6000, it simply renders one control per
page, which unfortunately is not what the users need. The want
controls to be continuous even if it means getting "broken" by a page
break. I picked 6000 because I was estimating the pixel height of the
page (600dpi * 10 inches). I'm hoping that I can do something to get
the exact height of each page down to the pixel
(GetDeviceCaps/GetHardMargins perhaps?).
2) In PrintPage, I won't know ahead of time how many pages are needed.
I'm currently just testing by hard-coding "mCount < 2". One thought I
had was to first write all graphics to a "cloned" DC using
CreateCompatibleDC --- I would then be able to see the total height by
looking at the final value of "y". I would then divide this by the
height that was determined using GetDeviceCaps/GetHardMargins.
I will try these, but if you have additional suggestions, you will
further solidify your legendary status in this office ;-)
Thanks....
Bob Powell [MVP] wrote:
I can't understand why you're getting an hDC from the Graphics of the
page,
passing that to the control which then creates a Graphics.FromDC object
to
render itself to.
Why not simply pass the original Graphics object?? Possibly with a
transform
pre-set.
You're obviously messing up the handles somewhere...
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Jeff" <jrush@xxxxxxx> wrote in message
news:1163649308.224660.274540@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thanks, I really appreciate the help. I'll try using the
matrix/transform.
I'm getting an error in my btnPrint.click event when it executes the
following code:
PrintDocument1.Print( )
The error is: "the handle is invalid"
The first page gets printed successfully; the second page never gets
spooled.
Any ideas what this could be?
Bob Powell [MVP] wrote:
PictureBox (using FromImage) as you've suggested.I've read your FAQ many times, and I'm using the image portion of
the
Ok, I'll let you off then ;-)
The rendering origin needs to be used as a negative transform to
offset
the
origin to _above_ the page such as:
Matrix mx=new Matrix(1,0,0,1,0,-y);
DCGraphic.Transform=mx;
You _should_ really be scaling to the printer page using the transform
too.
Doing this would mean that the objects could just draw to a DC without
worrying whether it was to a printer or the screen.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Jeff" <jrush@xxxxxxx> wrote in message
news:1163625848.369228.29310@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I've read your FAQ many times, and I'm using the image portion of
the
PictureBox (using FromImage) as you've suggested.
Regarding your other suggestion...
Here is the code I have in the PrintPage event. Each "ctrl"
performs
whatever operations it needs onto the device context (DrawRectangle,
DrawString, etc), and in addition keeps track of its own height in
terms of the DC's pixels (600 for printer, 96 for display).
y = <starting Y position>
For Each ctrl In mControlList
hDc = e.Graphics.GetHdc
ctrl.RenderToDC(hDc, y)
e.Graphics.ReleaseHdc()
y = y + ctrl.ControlHeight
Next
In each control's RenderDC method, I have the following:
DCgraphic = Graphics.FromHdc(hdc)
DCgraphic.PageUnit = GraphicsUnit.Pixel
DCgraphic.RenderingOrigin = New Point(0, y)
...where "y" is the same "y" from the calling codeblock. Perhaps
the
"RenderingOrigin" line needs to go in the calling app's PrintPage
event?
Thanks for your help.
Bob Powell [MVP] wrote:
(usually a PictureBox)
_groan_ you didn't read the FAQ did you??
You need to figure out how many pages you need and then render your
graphic
one time for each page. You can optimise away objects that do not
coincide
with the currently displayed area or you can just render it all
with a
different origin for each page.
The origin could be an offset in a global transform.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Jeff" <jrush@xxxxxxx> wrote in message
news:1163608248.003096.193460@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
(this is a "scaled down" version of a previous post to hopefully
attract more responses)
Scenario:
1) I have a class that renders graphics and text onto a passed-in
HDC.
2) The class doesn't know or care if the DC is a display (usually
a
PictureBox) or a printer
3) The "size" of the graphics/text can be very large, spanning
many
pages
If the graphics/text that is rendered spans multiple pages, the
problem
I have with using the PrintDocument/PrintPage/HasMorePages is
that
it
doesn't know when to go to the next page. Why? Because inside
the
PrintPage method, I have passed the HDC to an external class that
doesn't care about page breaks.
When the HDC is a display, I don't have a problem because my
PictureBox
height can go up to 32767 pixels (which at 96dpi is roughly 28
feet).
The user can scroll to see everything.
However, when the HDC is a printer, a page is only 11 inches
high.
Am I going to have to modify my class to take into consideration
whether or not the DC is a printer? Even if I do, let's say I
need
to
draw a rectangle that is 5 inches tall and starts near the bottom
of
a
page: how do I "break up" the rectangle into two pieces (one on
each
page)?
.
- References:
- Printing graphics/text that span multiple pages
- From: Jeff
- Re: Printing graphics/text that span multiple pages
- From: Bob Powell [MVP]
- Re: Printing graphics/text that span multiple pages
- From: Jeff
- Re: Printing graphics/text that span multiple pages
- From: Bob Powell [MVP]
- Re: Printing graphics/text that span multiple pages
- From: Jeff
- Re: Printing graphics/text that span multiple pages
- From: Bob Powell [MVP]
- Re: Printing graphics/text that span multiple pages
- From: Jeff
- Printing graphics/text that span multiple pages
- Prev by Date: Re: Display text in textbox with leaving space from both ends.
- Next by Date: Printing using (very) narrow fonts
- Previous by thread: Re: Printing graphics/text that span multiple pages
- Next by thread: what's wrong here: scan pass for transparency
- Index(es):
Relevant Pages
|
Loading