Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hi
i am a newbie i am using vs 2010. I have a windows form with a background image. And i have some label on that
I want to stamp that form with labels on that.
How can i do that?
I have seached a lot but. but i am confused how to do that as there are so man tools i.ei dont know what is this all thing but how can i stamp a form which labels on it?

Thanks in advance
Posted 15-Nov-12 2:37am
sariqkhan1.4K
Edited 16-Dec-12 19:00pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The easiest way is to use the PrintForm component from VB powerpacks: it's only a couple of lines of code. http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.powerpacks.printing.printform.aspx[^]

But you will get better results if you use a PrintDocument - it's a bit more to get your head around, but it produces a much, much more flexible and generally better result: http://msdn.microsoft.com/en-us/library/6he9hz8c.aspx[^]

Both of the links provide an example.

[edit]Typo: PrintDocuiment for PrintDocument - OriginalGriff[/edit]
  Permalink  
v2
Comments
sariqkhan at 15-Nov-12 8:00am
   
what is the diffrence between vb's printform and vs's printdocument?
if i have a small scale form. means it does only have a photo and labels on it so which method you will prefer? vb or vs?
OriginalGriff at 15-Nov-12 8:19am
   
I would always go with PrintDocument - it takes a bit more work, =but you get a better result.
shaikh-adil at 15-Nov-12 8:16am
   
i am also doing this since one week that article is dumb. the code is not correct i think.
this is my code which is copied from that website using print document
there is some error
public partial class Form1 : Form
{
public Form1()
{

private Button printButton = new Button();
private PrintDocument printDocument1 = new PrintDocument();

public Form1()
{
printButton.Text = "Print Form";
printButton.Click += new EventHandler(printButton_Click);
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);//here is the error that "Ambiguity between 'printing.Form1.printDocument1' and 'printing.Form1.printDocument1'
this.Controls.Add(printButton);
}

void printButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print(); // here is the error that "Ambiguity between 'printing.Form1.printDocument1' and 'printing.Form1.printDocument1'
}


Bitmap memoryImage;

private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}

private void printDocument1_PrintPage(System.Object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}



public static void Main()
{
Application.Run(new Form1());
}
}
}



public static void Main()
{
Application.Run(new Form1());
}
OriginalGriff at 15-Nov-12 8:22am
   
Somehow, you have two objects called printDocument1 in your form - which is not easy to do, unless you have added one to the form as a control at design time, and added one via the line
private PrintDocument printDocument1 = new PrintDocument();
near the top of your code. If you have, then remove one of them - and I suspect you want to add the Button via the designer as well, or it won't get displayed! :laugh:
shaikh-adil at 15-Nov-12 9:02am
   
sorry i have solved but i dont have printer istalled. what type of output i will get??
if i want only the picturebox and there is label on that picturebox to be printed so how to do that?
i dont want whole form if the close minimize button and maximize button will be printing on that page
OriginalGriff at 15-Nov-12 9:32am
   
Probably none!
Install a PDF writer (This one is free: http://www.cutepdf.com/products/cutepdf/writer.asp) and it will act as a printer and write to a file as a PDF document - saves a lot of paper when developing print apps! :laugh:

I would still use a PrintDocument - it's only two lines of code in teh actual print method: one using e.DrawImage and one using e.DrawString to draw your picture and label conttnt.
shaikh-adil at 15-Nov-12 12:55pm
   
Probably none!????
i have pasted above lines of code.?it will not produce any image??
and can you helpme with code if i have a picturebox and lables. then where will be modification in coding?
OriginalGriff at 15-Nov-12 13:59pm
   
"dont have printer istalled. what type of output i will get?" - "Probably none!"

If you don't have a printer, it can't produce any image...
The code you show above won't do anything, because the button you have linked to the click event handler is not visible on the form!
sariqkhan at 17-Nov-12 3:21am
   
bro i get the output which i dont want. it prints the whole form with close minimize and maximize button. i want only the picturebox and label on that to be printed on paper. so how to do that?
can you guide me?
OriginalGriff at 17-Nov-12 3:24am
   
As I said: use a PrintDocument, not a PrintForm.
PrintForm prints the form - i.e. the title bar, boxes, etc. as well. You can't turn that off...
sariqkhan at 17-Nov-12 6:51am
   
yess sir i have used the above code. and it is printing the whole form. now you can guide me to edit the page to only print the picturebox and labels on that.
where i have to edit the code
OriginalGriff at 17-Nov-12 7:06am
   
Do you mean that you are capturing the screen, printing it, and wondering how you get rid of the windows form parts? If so, why?
Why not just print the image and labels yourself?
All you have to do is:
1) Stop capturing your screen.
2) Use the DrawImage method call in your printDocument1_PrintPage method to print MyPictureBox.Image (or whatever name you have given it) instead of memoryImage.
3) Then use DrawString to add your labels.
shaikh-adil at 17-Nov-12 6:53am
   
i am too asking that only
sariqkhan at 17-Nov-12 10:50am
   
sorry sir i am totally new to this concept can you write the code. Please i have problem in understanding verbally
OriginalGriff at 17-Nov-12 11:12am
   
Replace your PrintPage:
private Font drawFont = new Font("Arial", 16);
private void printDocument1_PrintPage(Object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(pictureBox1.Image, 0, 0);
e.Graphics.DrawString(label1.Text, drawFont, Brushes.Black, new PointF(10, 10));
e.HasMorePages = false;
}
sariqkhan at 18-Nov-12 2:03am
   
thanks bro you have really helped us
:)
OriginalGriff at 18-Nov-12 3:10am
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
sariqkhan at 15-Nov-12 9:10am
   
but i dont have printer istalled. what type of output i will get?? if i want only the picturebox and there is label on that picturebox to be printed so how to do that? i dont want whole form if the close minimize button and maximize button will be printing on that page
also asked by adil
shaikh-adil at 17-Nov-12 6:54am
   
how to edit the form to print only the picture box? and labels on that form

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Suvendu Shekhar Giri 265
1 Sergey Alexandrovich Kryukov 260
2 Andy Lanng 185
3 PIEBALDconsult 180
4 OriginalGriff 130
0 Sergey Alexandrovich Kryukov 6,425
1 OriginalGriff 5,918
2 Peter Leow 2,514
3 Maciej Los 2,263
4 Abhinav S 2,249


Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100