Printing controls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 
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."
 
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
 
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
 
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);
}
}


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
 
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."
 
Back
Top