473,386 Members | 1,821 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,386 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 10176
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.