 |
|
 |
Great job with this project. It works fine for me in Windows 7. However when executing in XP, it will print the tree up to the height of the window, then display a big black band until the end of the tree. Any suggestions why this may be the case? regards
|
|
|
|
 |
|
 |
Thanks for the good work ...
I want to share this, maybee it helps someone ...
I had a problem with printing very big Treeviews, over a few pages.
After a few pages, the last node was cut of a little bit.
It seams on every page we "lost" a pixel, so ich change the code a little, and it works well ...
Search for "lastPrintPosition.Y +=" and replace
"_lastPrintPosition.Y += (sourceRect.Height + 1);"
with
"_lastPrintPosition.Y += (sourceRect.Height);
Regards, AIschA
|
|
|
|
 |
|
 |
Excellent work ... plus the print error solution regarding removing the _EndPrint event.
|
|
|
|
 |
|
 |
Hello,
Thank you and thanks to Carl Vaillancourt for the tip about the print-error!
Best regards,
Alexander
|
|
|
|
 |
|
 |
works perfectly. well written !
|
|
|
|
 |
|
 |
I downloaded this application and am running VS2008. I did a standard upgrade. When i ran the application I am seeing the tree view in forms dialog. When I look at the print preview and then click on the printer icon the applicatoin stops at: g.DrawImage(this._controlImage, destRect, sourceRect, GraphicsUnit.Pixel); and reports an invalid argument. Can anyone help me with this? Thanks
|
|
|
|
 |
|
 |
When the treeview height reach its maximum value (32767) the CreateCompatibleBitmap returns 0 value.
And so Bitmap.FromHbitmap(hbm) creates generic error.
|
|
|
|
 |
|
 |
I downloaded this application and am running VS2008. I did a standard upgrade. When i ran the application I am seeing the tree view in forms dialog. When I look at the print preview and then click on the printer icon the applicatoin stops at: g.DrawImage(this._controlImage, destRect, sourceRect, GraphicsUnit.Pixel); and reports an invalid argument. Can anyone help me with this? Thanks
|
|
|
|
 |
|
|
 |
|
 |
Hi,
Thank you a lot for the good ideas in this article.
I used your way of getting an image of an control to generate an png of nested UserControls (.NET 1.1).
But when i call GetImage() recursiv, the code produces exceptions. I solved the problem by adding DeleteObject(hbm).
Maybe this is an error that only occurs in some freaky use case, but i monitored some activity in the comment section of this article, so maybe this bugfix helps the next programmer.
private Image GetImage(IntPtr handle, int width, int height)
{
IntPtr screenDC = GetDC(IntPtr.Zero);
IntPtr hbm = CreateCompatibleBitmap(screenDC, width, height);
Image image = Bitmap.FromHbitmap(hbm);
Graphics g = Graphics.FromImage(image);
IntPtr hdc = g.GetHdc();
SendMessage(handle, 0x0318 /*WM_PRINTCLIENT*/, hdc, (0x00000010 | 0x00000004 | 0x00000002));
g.ReleaseHdc(hdc);
DeleteObject(hbm);
ReleaseDC(IntPtr.Zero, screenDC);
return image;
}
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
Again, thanks a alot for sharing your work.
fiftyfifty
|
|
|
|
 |
|
 |
I get the same thing as above , has anyone found a solution for it ?
|
|
|
|
 |
|
 |
For some reasons, it seems that the end print event is raised too fast, so the _controlImage.Dispose(); is launched before the print even begins, and thats why we get this error.... try removing that line in the _printDoc_EndPrint methode. It worked for me.
|
|
|
|
 |
|
 |
that 's very wonderfull!thank alot!
|
|
|
|
 |
|
 |
I got problem when try to execute
g.DrawImage(this._controlImage, destRect, sourceRect, GraphicsUnit.Pixel);
it says invalid parameter. I try to save the image still it gives me same error. I am not understading the problem.
Below text is junk while running the exe. Sorry if it couln't help you.
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.get_RawFormat()
at System.Drawing.Graphics.IgnoreMetafileErrors(Image image, Int32& errorStatus)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit)
at Pitman.Printing.PrintHelper.printDoc_PrintPage(Object sender, PrintPageEventArgs e) in G:\Desktop\PrintTreeViewSample\PrintTreeViewSample\Pitman.Printing\PrintHelper.cs:line 164
at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e)
at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)
at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
at System.Windows.Forms.PrintPreviewDialog.OnprintToolStripButtonClick(Object sender, EventArgs e)
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
PrintTreeViewSample
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///G:/Desktop/PrintTreeViewSample/PrintTreeViewSample/PrintTreeViewSample/bin/Debug/PrintTreeViewSample.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Pitman.Printing
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///G:/Desktop/PrintTreeViewSample/PrintTreeViewSample/PrintTreeViewSample/bin/Debug/Pitman.Printing.DLL
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
|
|
|
|
 |
|
 |
well, first of all congratulations, I think the control is amazing. I'm only running in a small problem that I can't quite solve: when I start the print preview it prints everything correctly, but at the end of the tree (not on the footer) it prints a black bar spanning the width of th tree and about a quarte inch in height. Any idea of what might be causing it? I'm using vs2003 and including the code directly into the app (not compiled into a dll).
Thanks
|
|
|
|
 |
|
 |
Can we do print the tree without the nodes icons?
|
|
|
|
 |
|
 |
The code worked for my application.
Thanks.
|
|
|
|
 |
|
 |
ok cool! i finally discovered the big bug
the problem with the existing code is as follows:
when PrintPreviewTree(tree, title) is run, you call
private void PrepareTreeImage(TreeView tree)
to initialise _controlImage. the problem comes when the "print preview" has finished loading and
the
void _printDoc_EndPrint(object sender, PrintEventArgs e)
{
_controlImage.Dispose();
}
method is run.
then when i want to print the document, the printDoc_PrintPage method tries to use _controlImage
via
g.DrawImage(this._controlImage, destRect, sourceRect, GraphicsUnit.Pixel);
problem is, it's been disposed, so the mango hits the fan and the app crashes.
solution:
-create a global variable
private TreeView _tree;
-in PrintPreviewTree(tree, title) method, don't just call PrepareTreeImage(TreeView tree), but
also assign _tree = tree.
-_printDoc_EndPrint: instead of _controlImage.Dispose(), use _controlImage = null
-in printDoc_PrintPage, place
if (_controlImage == null)
PrepareTreeImage((TreeView)_tree);
just above the g.DrawImage(this._controlImage, destRect, sourceRect, GraphicsUnit.Pixel);
e voila, it prints .
(now i still need to figure out why it infinite loops to my actual printer )
|
|
|
|
 |
|
 |
when i try to print preview, it just keeps adding more and more pages infinitely (as if the document is infinitely long). does anyone know where i can find out how to fix this or what the cause might have been?
it seems to be related to a "generic error in GDI+" error. i can provide more details if needed
-- modified at 8:03 Monday 5th February, 2007
|
|
|
|
 |
|
 |
How big of a TreeView are you trying to print?
Mark Pitman
"After three days without programming, life becomes meaningless." - The Tao of Programming
|
|
|
|
 |
|
 |
only about a quarter of a page (bout 10 times smaller than the source code's printout). but it doesn't matter, cause the source code does the same. it only does this when my default printer is an "actual" printer. when i make the default printer "Microsoft Office Document Image Writer", it doesn't go into an infinite loop for the preview, but gives an
"ArgumentException" - Parameter is not valid.
when trying to do the actual printing at the following line:
g.DrawImage(this._controlImage, destRect, sourceRect, GraphicsUnit.Pixel); //printDoc_PrintPage method
the cause is "this._controlImage" (i've made no change to the code whatsoever - dl'ed it yesterday)
any clues? it makes no sense to me whatsoever.
even "newby mistake" suggestions are welcome - who knows, i might have missed something simple
|
|
|
|
 |
|
 |
ok, so all i actually managed to do was figure out that not all printers will give you the infinite loop problem. the cause of the problem is the
System.Drawing.Printing.PrintPageEventArgs e
parameter in
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
the e.MarginBounds gives negative height and width values and the e.pagebounds rectangle says the pagesize is 0. those negative values are what causes the infinite loop of printing to a non-existing page. fortunately this was not the case for a different printer i tested on just now.
if anyone else also gets this problem, don't feel alone - i gave up. (btw, both printers were black & white laser printers, so that's not the source of the problem. - furthermore, the infinite loop printer prints fine from other apps, so i can't think of any certain settings causing probs)
does anyone know if there is a limit to System.Drawing.Printing.PrintPageEventArgs library somehow that could cause this?
|
|
|
|
 |
|
 |
Very usefull.
~dino martino~
|
|
|
|
 |
|
 |
Of, No...
I found your article too late
|
|
|
|
 |
|
 |
I had pretty much abandoned this article, but when I got a couple of emails about it recently, I thought I should update it. Hopefully the update will be posted soon.
Mark Pitman
"After three days without programming, life becomes meaningless." - The Tao of Programming
|
|
|
|
 |