473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DrawToBitmap doesn't work on panel

Fr33dan
57
So I've written a class that has a stores some test results info and then a control that displays that info to the user. I want to put a print function on this class to draw the control at a full page size and print it. However it always comes out blank. The code see the panel as a control because it could be some other type of value. I figure there must be something simple I'm missing.
Expand|Select|Wrap|Line Numbers
  1. void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  2.         {
  3.             System.Drawing.Size oldSize = printData.Size;
  4.  
  5.             printData.Size = new System.Drawing.Size(e.MarginBounds.Width, e.MarginBounds.Height);
  6.             System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Size.Width, printData.Size.Height);
  7.  
  8.             InvertZOrderOfControls(printData.Controls);
  9.             printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, printData.Size.Width, printData.Size.Height));
  10.             InvertZOrderOfControls(printData.Controls);
  11.  
  12.             e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
  13.             bitmap.Save(@"C:\Users\jdudley\Documents\File.bmp");
  14.             printData.Size = oldSize;
  15.         }
The save call was added for debugging. It looks like it's actually rendering the background color of the panel without any of the controls.
Jun 12 '12 #1
2 7513
Plater
7,872 Expert 4TB
Did you try taking out the zorder change and see if it helps?
Jul 2 '12 #2
Fr33dan
57
Sorry, I forgot about this post when no one responded and went to another forum.

It turns out a control can only be printed after it has been added window and drawn. In order to get this code to work as intended I had to show the item off screen before drawing to a bitmap. But in case the item was already being show on a dialog I had to include code to re-add it to it's original parent.

Expand|Select|Wrap|Line Numbers
  1. void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  2. {
  3.     System.Drawing.Size oldSize = printData.Size;
  4.     System.Drawing.Color oldBackcolor = printData.BackColor;
  5.     Control oldParent = printData.Parent;
  6.     printData.Size = new System.Drawing.Size(e.MarginBounds.Width, e.MarginBounds.Height);
  7.     printData.BackColor = Color.White;
  8.  
  9.     if (oldParent != null)
  10.         oldParent.Controls.Remove(printData);
  11.  
  12.     handleForm = new Form();
  13.     handleForm.Controls.Add(printData);
  14.     handleForm.StartPosition = FormStartPosition.Manual;
  15.     handleForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  16.  
  17.     handleForm.Show();
  18.  
  19.     Bitmap bitmap = new Bitmap(printData.Width, printData.Height);
  20.     printData.DrawToBitmap(bitmap, new Rectangle(new Point(0, 0), printData.Size));
  21.  
  22.     e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
  23.  
  24.     handleForm.Controls.Remove(printData);
  25.     if (oldParent != null)
  26.         oldParent.Controls.Add(printData);
  27.     handleForm.Close();
  28.  
  29.     printData.Size = oldSize;
  30.     printData.BackColor = oldBackcolor;
  31. }
Jul 2 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes...
2
by: George Hester | last post by:
I have a page that loads an applet. It's a cube that is used as a navigation menu. This issue actually happens in both Microsoft Internet Explorer and Netscape. All versions that I know of. And...
5
by: Gary Mayor | last post by:
Hi, If I have the ' character within the javascript:pick command it doesn't work. Is there some sort of way of escaping these characters like in server side languages. function pick(symbol) {...
7
by: Bennett Haselton | last post by:
Is there any way to find a string representing an object's class, which will work in Internet Explorer 6? "typeof" doesn't work -- it returns "object" for all objects: x =...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
4
by: bbp | last post by:
Hello, In an ASPX page I have a "Quit" button which make a simple redirect in code-behind. This button doesn't work no more since (I think) I moved from the framework 1.0 to 1.1 and it doesn't...
1
by: GM | last post by:
Hello, I have visual studio 2005 on XP SP2. I have a simple sample application using a webbrowser and 2 buttons. Button1 loads the page Button2 saves the screenshot But for some reason the...
10
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET...
1
by: Phillip Taylor | last post by:
I have a panel which has a textbox and a picture box on it. When the resize event of the panel fires, I have code: Protected Overrides Sub OnResize(ByVal eventargs As System.EventArgs)...
1
by: windscar | last post by:
Hello Everybody! I am a beginner that try to learn C++ programming. I work in Linux Ubuntu Karmic Koala (9.10) environment. I got a problem with getchar(). The function works well in a simple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.