2

I need help printing the form. I used the ControlPrinter class How to use PrintDocument with a scrollable Panel? , I didn't make any changes to it, and it only prints the background of the form anyway.Please advise me what I did wrong. Thank you in advance

private void button1_Click(object sender, EventArgs e)
    {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
        pd.Print();


    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        var bitmap = ControlPrinter.ScrollableControlToBitmap(this, true, true);
        e.Graphics.DrawImage(bitmap, 0, 0);
    }
petule.08
  • 41
  • 4
  • Yes, there is a little bug in the code. In the `DrawNestedControls` method, find `if (parent == outerContainer) { clipBounds = ctl.Bounds; }` and comment the next `else` or replace it with `else if (parent != ctl) { ..` and remove the inner `if (parent != ctl)`. FYI: @Jimi – dr.null Feb 08 '22 at 20:03
  • Hi, I made an adjustment but nothing has changed I still have only the background form printed. Adjustment: `if (parent == outerContainer) { clipBounds = ctl.Bounds; }` `else if (parent != ctl)` `{if (clipBounds.Width < 1 || clipBounds.Height < 1) continue; ....` @dr.null – petule.08 Feb 09 '22 at 08:08
  • 1
    Try [this](https://pastebin.com/db0D9rum) version and keep an eye on the original post for revisions. – dr.null Feb 09 '22 at 11:06
  • Also, `bitmap.Dispose();` after drawing it. – dr.null Feb 09 '22 at 11:10
  • 1
    Sorry. I made some changes directly in the question's edit box that caused a problem (the code wouldn't compile either). It should be fixed now. If you find other bugs, let me know. Thanks to @dr.null – Jimi Feb 09 '22 at 12:09
  • Thanks, with your code editing, it already renders. Things that did not fit into the field of view are not drawn. You don't know what to do with it?@dr.null – petule.08 Feb 10 '22 at 08:46
  • I changed the previous behavior a bit. Take the last edit. -- With `ScrollableControlToBitmap(this, true, false)` or `ScrollableControlToBitmap(this, true, true)` it does print the whole content of a Form, no matter the current size. – Jimi Feb 10 '22 at 14:20
  • It doesn't work and it throws an error at DrawNestedControls `ctl.DrawToBitmap(bitmap, bounds);` @Jimi – petule.08 Feb 12 '22 at 08:49
  • Just tested the class as posted (copy/pasted) in .Net Framewrok 4.8 and .Net 5 and I don't have any exception. -- Can you specify what kind of exception you get and, possibly, post the Form you're using this with somewhere, so it can be tested? -- I'll give it a look as soon as possible anyway. – Jimi Feb 12 '22 at 09:58
  • when shrinking the form it threw an error at bounds @Jimi – petule.08 Feb 16 '22 at 09:32
  • I had no time to check it out. When I do, I'm posting a comment here. – Jimi Feb 16 '22 at 09:43
  • I would like to ask what this code does and why it is not used anywhere you are not referring to it in the program ` public Rectangle ToRectangle() => Rectangle.FromLTRB(Left, Top, Right, Bottom);` @Jimi – petule.08 May 04 '22 at 07:42
  • could you help me adjust this printing program so that the printed form adapts to A4 or A3 size automatically @dr.null – petule.08 May 04 '22 at 08:02
  • I need help with editing this printing program i have in the form chart and toolStrip and I am getting an error System.ArgumentException: 'targetBounds' in `ctl.DrawToBitmap(bitmap, bounds);` @Jimi – petule.08 May 04 '22 at 08:53
  • See [this](https://stackoverflow.com/questions/684801/setting-the-paper-size/32723641#32723641) for the PaperSize part. As for the exception, before you call `ctl.DrawToBitmap(bitmap, bounds);` add an `if` statement to check the size of the passed bounds, don't call the method or skip ( `continue;` ) if you get `if (bounds.Width == 0 || bounds.Height == 0)`. – dr.null May 04 '22 at 19:49
  • And maybe `if (bounds.Width <= 0 || bounds.Height <= 0) continue;` – dr.null May 05 '22 at 20:08
  • Sorry wrong I described the problem. When I print the form it does not fit on A4 and part is not there and the whole is printed on A3. There is a possibility that the program adjusts the size for printing so that it fits A4 so that it does not interfere with the size of the form itself @dr.null – petule.08 May 10 '22 at 07:37
  • I tried your edit and we still throw the same mistake. System.ArgumentException: 'targetBounds' in `ctl.DrawToBitmap(bitmap, bounds);` – petule.08 May 10 '22 at 07:47
  • To fit the target area (whether `e.MarginBounds`, `e.PageBounds`, or `e.Graphics.VisibleClipBounds`....) use the `e.Graphics.DrawImage` overload that takes the destination and source rectangles params. As for the exception, I don't know, you need to post the code to see what's happening. – dr.null May 10 '22 at 08:50
  • I will try to send the code. the class works fine with standard elements but as I insert the chart there it starts to throw an error. Is it possible that he can't know that it can't be used on charts? @dr.null – petule.08 May 11 '22 at 09:19
  • hi edited i added to the code `if (ctl.Bounds.Width <= 0 || ctl.Bounds.Height <= 0) ctl.DrawToBitmap(bitmap, bounds);` now it only prints the background form. @dr.null – petule.08 May 12 '22 at 08:33
  • I told you to do the opposite :). You should `continue` if you have this condition. Anyway, change it to: `if (bounds.Width > 0 && bounds.Height > 0) ctl.DrawToBitmap(bitmap, bounds);`. Check `bounds` not `ctl.Bounds` in case they don't have the same value. – dr.null May 12 '22 at 09:29
  • When I apply your edit it still reports an error System.ArgumentException: 'targetBounds' . When I adjust the sign to – petule.08 May 16 '22 at 08:07
  • Hi, I found that when some elements are placed on a form and some in a splitconteiner it throws a targetbound error. Do you have an idea how to modify it so that I can have elements outside the split container and can print it? @dr.null – petule.08 May 19 '22 at 08:33
  • Hi i needed help to improve the quality of the printed bitmap i tried e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; but it did not help. you will probably need to modify your code but unfortunately I don't know how. could you advise me. Thank you. @Jimi – petule.08 Jun 21 '22 at 08:21
  • Smoothing is not exactly what you want when printing (quite different than *showing*). You should increment / adapt the DPI descriptor to match the DPI of the Printer and possibly increase the scale of the printed objects, which may introduce discrepancies, since not all objects print to Bitmap the same way (notable the RichTextBox control, which needs handling in a different way entirely) -- That piece of code is not *production grade*, it's a base you can use to build on. Anyway, it also has a couple of glitches I have to fix. Will do if I have the time. – Jimi Jun 21 '22 at 11:39

0 Answers0