473,322 Members | 1,523 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,322 software developers and data experts.

Printing controls

Hi!

I have a panel formed as an A4-page, in this panel I have som labels,
pictureboxes and a datagrid. How can I print the complete panel vith all it's
controls?

Regards
Anders Aleborg
Nov 16 '05 #1
7 10172
Hi Anders,

Try this link from MSDN:
http://msdn.microsoft.com/library/en...intsupport.asp

There are a number of samples including one on datagrid as well.

HTH,
Rakesh Rajan

"an****@aleborg.se" wrote:
Hi!

I have a panel formed as an A4-page, in this panel I have som labels,
pictureboxes and a datagrid. How can I print the complete panel vith all it's
controls?

Regards
Anders Aleborg

Nov 16 '05 #2
All I can find is how to print a complete form, text or graphics not anything
about different controls. I've managed to print a datagrid but it only ends
up in the left corner, not where I tried to place it using location.

"Rakesh Rajan" wrote:
Hi Anders,

Try this link from MSDN:
http://msdn.microsoft.com/library/en...intsupport.asp

There are a number of samples including one on datagrid as well.

HTH,
Rakesh Rajan

"an****@aleborg.se" wrote:
Hi!

I have a panel formed as an A4-page, in this panel I have som labels,
pictureboxes and a datagrid. How can I print the complete panel vith all it's
controls?

Regards
Anders Aleborg

Nov 16 '05 #3
Hi Anders,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to print the controls in the
panel to a printer. If there is any misunderstanding, please feel free to
let me know.

Based on my experience, we have to achieve this with 2 step. First, we've
got to convert the panel control to a bitmap image. Second, we print it to
the printer.

Here is a code snippet that can do the convertion from certain control to a
bitmap.

Private Sub MenuItem2_Select(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MenuItem2.Select
Dim g1 As Graphics
g1 = Me.CreateGraphics()
Dim MyImage As Image
MyImage = New Bitmap(Me.Width, Me.Height, g1)
Dim a As Int32
a = Me.Height
Dim g2 As Graphics
g2 = Graphics.FromImage(MyImage)
Dim dc1 As IntPtr
dc1 = g1.GetHdc()
Dim dc2 As IntPtr
dc2 = g2.GetHdc()
BitBlt(dc2, 0, 0, Me.Width, Me.Height, dc1, -4,
Me.ClientRectangle.Height - Me.Height + 4, 13369376)
g1.ReleaseHdc(dc1)
g2.ReleaseHdc(dc2)
Dim p As Point
p.X = Me.MousePosition.X - Me.Location.X
p.Y = Me.MousePosition.Y - Me.Location.Y
Me.Cursor.Draw(g2, New Rectangle(p, Me.Cursor.Size))
MyImage.Save("c:\saved.jpg", Drawing.Imaging.ImageFormat.Jpeg)
MessageBox.Show("Finished Saving Image")
End Sub

Private Declare Auto Function BitBlt Lib "GDI32.DLL" _
(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 Int32) As Boolean

If you just need to print it, you needn't save it to a file. Just pass it
to print document as the MSDN document mentioned that Rakesh provided.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #4
Hi Kevin, thanks!
Correct me if I'm wrong, your code saves the form including the mousepointer
to a file :)
So let's say I need to get a panel into the image instead and some labels,
how do I add the labels to the image?
The code in C# without the mousepointer:

Graphics g1 = this.CreateGraphics();
Image MyImage = new Bitmap(panel1.Width,panel1.Height,g1);
int a = panel1.Height;

Graphics g2 = Graphics.FromImage(MyImage);
System.IntPtr dc1 = g1.GetHdc();
System.IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, panel1.Width, panel1.Height, dc1, -4,
this.ClientRectangle.Height - panel1.Height + 4, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);

How do I translate this to C#?
Private Declare Auto Function BitBlt Lib "GDI32.DLL" _
(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 Int32) As Boolean

Regards
Anders ALeborg

"Kevin Yu [MSFT]" wrote:
Hi Anders,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to print the controls in the
panel to a printer. If there is any misunderstanding, please feel free to
let me know.

Based on my experience, we have to achieve this with 2 step. First, we've
got to convert the panel control to a bitmap image. Second, we print it to
the printer.

Here is a code snippet that can do the convertion from certain control to a
bitmap.

Private Sub MenuItem2_Select(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MenuItem2.Select
Dim g1 As Graphics
g1 = Me.CreateGraphics()
Dim MyImage As Image
MyImage = New Bitmap(Me.Width, Me.Height, g1)
Dim a As Int32
a = Me.Height
Dim g2 As Graphics
g2 = Graphics.FromImage(MyImage)
Dim dc1 As IntPtr
dc1 = g1.GetHdc()
Dim dc2 As IntPtr
dc2 = g2.GetHdc()
BitBlt(dc2, 0, 0, Me.Width, Me.Height, dc1, -4,
Me.ClientRectangle.Height - Me.Height + 4, 13369376)
g1.ReleaseHdc(dc1)
g2.ReleaseHdc(dc2)
Dim p As Point
p.X = Me.MousePosition.X - Me.Location.X
p.Y = Me.MousePosition.Y - Me.Location.Y
Me.Cursor.Draw(g2, New Rectangle(p, Me.Cursor.Size))
MyImage.Save("c:\saved.jpg", Drawing.Imaging.ImageFormat.Jpeg)
MessageBox.Show("Finished Saving Image")
End Sub

Private Declare Auto Function BitBlt Lib "GDI32.DLL" _
(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 Int32) As Boolean

If you just need to print it, you needn't save it to a file. Just pass it
to print document as the MSDN document mentioned that Rakesh provided.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #5
Hi Anders,

For defination in C#:

[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
public static extern Bool BitBlt(
IntPtr hObject,
int nXDest, int nYDest,
int nWidth, int nHeight,
IntPtr hObjSource, int nXSrc, int nYSrc,
int dwRop);
Luke

Nov 16 '05 #6
Hi!

I solved it in another way:

private void DrawAll(Graphics g)
{
RectangleF srcRect = new Rectangle(0, 0, this.panel1.Width,
panel1.Height);
int nWidth = 802;
int nHeight = 1104;
RectangleF destRect = new Rectangle(0, 0, nWidth, nHeight);

float scalex = destRect.Width/this.panel1.Width;
float scaley = destRect.Height/this.panel1.Height;

GraphicsUnit gu = GraphicsUnit.Pixel;
RectangleF scaledRectangle;
Brush br = new
System.Drawing.SolidBrush(Color.FromArgb(((System. Byte)(74)),
((System.Byte)(74)), ((System.Byte)(74))));

scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox1.Bounds);
Image my1Image = (Image)pictureBox1.Image.Clone();
g.DrawImage(my1Image, scaledRectangle,pictureBox1.Image.GetBounds(ref
gu), GraphicsUnit.Pixel);

scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox2.Bounds);
Image my2Image = (Image)pictureBox2.Image.Clone();
g.DrawImage(my2Image, scaledRectangle,pictureBox2.Image.GetBounds(ref
gu), GraphicsUnit.Pixel);

scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox3.Bounds);
Image my3Image = (Image)pictureBox3.Image.Clone();
g.DrawImage(my3Image, scaledRectangle,pictureBox3.Image.GetBounds(ref
gu), GraphicsUnit.Pixel);

if(pictureBox4.Visible && pictureBox5.Visible)
{
scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox4.Bounds);
Image my4Image = (Image)pictureBox4.Image.Clone();
g.DrawImage(my4Image, scaledRectangle,pictureBox4.Image.GetBounds(ref
gu), GraphicsUnit.Pixel);

scaledRectangle = GetScaledRectangle(scalex, scaley, pictureBox5.Bounds);
Image my5Image = (Image)pictureBox5.Image.Clone();
g.DrawImage(my5Image, scaledRectangle,pictureBox5.Image.GetBounds(ref
gu), GraphicsUnit.Pixel);
}

foreach(Control x in this.panel1.Controls)
{
if(x.GetType().ToString() == "System.Windows.Forms.Label" && x.Visible)
{
Label theText = (Label)x;
x.BringToFront();
StringFormat drawFormat = new StringFormat();
if(theText.TextAlign.ToString() == "TopRight")
{
drawFormat.Alignment = StringAlignment.Far;
g.DrawString(theText.Text, theText.Font,br ,
(theText.Bounds.Left+theText.Width+2)*scalex, theText.Bounds.Top * scaley,
drawFormat);
}
else if(theText.TextAlign.ToString() == "TopCenter")
{
drawFormat.Alignment = StringAlignment.Center;
g.DrawString(theText.Text, theText.Font,br ,
(theText.Bounds.Left+(theText.Width/2))*scalex, theText.Bounds.Top * scaley,
drawFormat);
}
else
g.DrawString(theText.Text, theText.Font,br ,
theText.Bounds.Left*scalex, theText.Bounds.Top * scaley, drawFormat);
}
}
"[MSFT]" wrote:
Hi Anders,

For defination in C#:

[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
public static extern Bool BitBlt(
IntPtr hObject,
int nXDest, int nYDest,
int nWidth, int nHeight,
IntPtr hObjSource, int nXSrc, int nYSrc,
int dwRop);
Luke

Nov 16 '05 #7
Hi Anders,

It was nice to hear that you have had the problem resolved. Thanks for
sharing your experience with all the people here. If you have any
questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dragan Kovac | last post by:
Hello everyone, I have a problem. I generated some kind of my own report (by building HTML file) and now I want to print it. Printing itself is not a problem, problem is with adding custom headers...
1
by: slothboy | last post by:
Hey guys, I'm having a little trouble printing subforms. I've attached the snippet of code that prints my form which contains various subforms. It prints bounded controls on the subform just...
4
by: Russ | last post by:
To ASP.NET printing experts: My Asp.net web form needs to print some reports at the client side. I've been trying to research this and find some confusing and conflicting information in previous...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
6
by: J Ames | last post by:
I have an ASP.NET (VB) form that has two drop downs, a horizontal rule and a button. The button invokes a stored procedure and several tables are created on the page with data populated. I want...
8
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web...
2
by: Brad Pears | last post by:
I have a vb.net 2005 application and am using the print preview screen. This screen has a printer icon on it that the user can use to print the document currently being viewed. It uses the default...
0
by: dotnet2005 | last post by:
Hi Toall , I want to explain you clearly , I am having some panels having some controls CheckBox,ComboBox,Datagridview,Hyperlink,label,Listbox,Picturebox,RadioButt­ on ,Textbox)...
18
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing...
1
by: varun sharma | last post by:
i have a C# windows application in Visual Studio. I have a form which is opened in a panel.i want to print the data in various controls of the form upon clicking a PRINT button.but i don't want to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.