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

Printing a WinForm as a Graphic

I'm using Visual Studio 2005 and C#. I need to print a WinForm used for data
entry as a graphic. In other words, I need to print the exact WinForm the
user sees on the screen.

I searched through the newsgroups, and found the code below. This seems
reasonable, except for the fact that the author does not provide his
SetSeq_Library. This is needed to load the Bitmap variable _ActiveFormImage.

Any suggestions on how to capture a WinForm as a Bitmap to feed to a
PrintPageEventHandler?

I like this solution much better than using GDI+, which was required in
Visual Studio 2003.

Thanks,
--
Randy
protected void PrintActiveForm(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

_ActiveFormImage = SetSeg_Library.SetCapture.Form(this);

PrtDoc.PrintPage += new PrintPageEventHandler(PrtDoc_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();

}

void PrtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(this._ActiveFormImage, 0, 0);
}

Jun 26 '06 #1
6 14876
Hi Randy,

Use the base Control method "DrawToBitmap" method.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
"randy1200" <ra*******@newsgroups.nospam> wrote in message
news:39**********************************@microsof t.com...
I'm using Visual Studio 2005 and C#. I need to print a WinForm used for
data
entry as a graphic. In other words, I need to print the exact WinForm the
user sees on the screen.

I searched through the newsgroups, and found the code below. This seems
reasonable, except for the fact that the author does not provide his
SetSeq_Library. This is needed to load the Bitmap variable
_ActiveFormImage.

Any suggestions on how to capture a WinForm as a Bitmap to feed to a
PrintPageEventHandler?

I like this solution much better than using GDI+, which was required in
Visual Studio 2003.

Thanks,
--
Randy
protected void PrintActiveForm(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

_ActiveFormImage = SetSeg_Library.SetCapture.Form(this);

PrtDoc.PrintPage += new PrintPageEventHandler(PrtDoc_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();

}

void PrtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(this._ActiveFormImage, 0, 0);
}

Jun 26 '06 #2
randy,

Why don't you just use DrawToBitmap method?
Just keep in mind that depending on the controls that you have on the form
you may or may not get the exact snapshot. Some controls cannot be drawn on
a bit map e.g. RichTextBox.
--
Stoitcho Goutsev (100)

"randy1200" <ra*******@newsgroups.nospam> wrote in message
news:39**********************************@microsof t.com...
I'm using Visual Studio 2005 and C#. I need to print a WinForm used for
data
entry as a graphic. In other words, I need to print the exact WinForm the
user sees on the screen.

I searched through the newsgroups, and found the code below. This seems
reasonable, except for the fact that the author does not provide his
SetSeq_Library. This is needed to load the Bitmap variable
_ActiveFormImage.

Any suggestions on how to capture a WinForm as a Bitmap to feed to a
PrintPageEventHandler?

I like this solution much better than using GDI+, which was required in
Visual Studio 2003.

Thanks,
--
Randy
protected void PrintActiveForm(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

_ActiveFormImage = SetSeg_Library.SetCapture.Form(this);

PrtDoc.PrintPage += new PrintPageEventHandler(PrtDoc_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();

}

void PrtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(this._ActiveFormImage, 0, 0);
}

Jun 26 '06 #3
The code below is what I came up with. Unfortunately, the contents of the
controls are blank. For instance, I have TextBox with some text. I see the
text on the screen, but the TextBox is empty when it prints using this code.
--
Randy
private Bitmap _ActiveFormImage;
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem = (ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

Rectangle r = new Rectangle(0, 0, this.Width, this.Height);
this.DrawToBitmap(_ActiveFormImage, r);

PrtDoc.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();
}

private void printPrevewToolStripMenuItem_Click(object sender,
EventArgs e)
{
printPreviewDialog1.ShowDialog();
}

"Kevin Spencer" wrote:
Hi Randy,

Use the base Control method "DrawToBitmap" method.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
"randy1200" <ra*******@newsgroups.nospam> wrote in message
news:39**********************************@microsof t.com...
I'm using Visual Studio 2005 and C#. I need to print a WinForm used for
data
entry as a graphic. In other words, I need to print the exact WinForm the
user sees on the screen.

I searched through the newsgroups, and found the code below. This seems
reasonable, except for the fact that the author does not provide his
SetSeq_Library. This is needed to load the Bitmap variable
_ActiveFormImage.

Any suggestions on how to capture a WinForm as a Bitmap to feed to a
PrintPageEventHandler?

I like this solution much better than using GDI+, which was required in
Visual Studio 2003.

Thanks,
--
Randy
protected void PrintActiveForm(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

_ActiveFormImage = SetSeg_Library.SetCapture.Form(this);

PrtDoc.PrintPage += new PrintPageEventHandler(PrtDoc_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();

}

void PrtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(this._ActiveFormImage, 0, 0);
}


Jun 26 '06 #4
You have a few things wrong here.

First, you never instantiate _ActiveFormImage. The DrawToBitmap method draws
on a Bitmap, not on null.

Second, you didn't post the crucial code, which does the actual printing.
That would be the event handler for the PrintDocument.PrintPage event. This
event passes a PrintPageEventArgs instance, which contains, among other
things, the Graphics device you need to draw on.

Third, you're not checking the available space to print on.

Fourth, you're printing, even if the user hits the Cancel button.

The process goes like this:
The PrintDocument contains all the information needed to print, including
things like the PageSettings, which contains the page orientation, margins,
etc., and the PrinterSettings, which is used to determine which printer to
use and what settings to give it. The PrinterSettings isn't nearly as
important as the PageSettings, because the PageSettings determines the
height and width of the page, the printable area of the page, the margins,
etc.

The PrintDialog class, like any other dialog, returns a DialogResult when it
is closed by either the OK or Cancle button. The value of this DialogResult
is used to determine whether to go forward or not.

When you call the Print method of the PrinterDocument, it fires the
PrintPage event, which passes a PrintPageEventArgs instance containing the
relevant data to print with. This includes the available area to print in,
which is specified by the PrintDocument's PageSettings property. You draw
with the Graphics object in the same way you would draw to any Graphics
object.

So, what you need to do is to remove the PrintDocument from the method it is
now encapsulated in, and make it into a field or property of the class, so
that you can (1) Make configuration changes if desired, to the print
configuration, and (2) measure the available space in the page, for the
purpose of making sure that your form is fully inside the page, and for
aligning it within the page.

I would wait to draw the Bitmap in the PrintPage event handler as a good
practice. This will ensure that the latest state of the form is printed,
regardless of any future changes you make to the code. In the event handler
for the PrintPage event, get the Bounds of the form, and instantiate your
Bitmap to that size. Take note of the height and width, as you may want to
change the orientation of the printing if, for example (as with most Forms)
the width is greater than the height, in which case you would probably want
to use Landscape orientation.

Once you have your Bitmap instantiated, call the DrawToBitmap method to draw
the Form on the Bitmap. Then get the Graphics from the PrintPageEventArgs
instance, and draw the Bitmap to the Graphics.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
"randy1200" <ra*******@newsgroups.nospam> wrote in message
news:13**********************************@microsof t.com...
The code below is what I came up with. Unfortunately, the contents of the
controls are blank. For instance, I have TextBox with some text. I see the
text on the screen, but the TextBox is empty when it prints using this
code.
--
Randy
private Bitmap _ActiveFormImage;
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem = (ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

Rectangle r = new Rectangle(0, 0, this.Width, this.Height);
this.DrawToBitmap(_ActiveFormImage, r);

PrtDoc.PrintPage += new
PrintPageEventHandler(printDocument1_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();
}

private void printPrevewToolStripMenuItem_Click(object sender,
EventArgs e)
{
printPreviewDialog1.ShowDialog();
}

"Kevin Spencer" wrote:
Hi Randy,

Use the base Control method "DrawToBitmap" method.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
"randy1200" <ra*******@newsgroups.nospam> wrote in message
news:39**********************************@microsof t.com...
> I'm using Visual Studio 2005 and C#. I need to print a WinForm used for
> data
> entry as a graphic. In other words, I need to print the exact WinForm
> the
> user sees on the screen.
>
> I searched through the newsgroups, and found the code below. This seems
> reasonable, except for the fact that the author does not provide his
> SetSeq_Library. This is needed to load the Bitmap variable
> _ActiveFormImage.
>
> Any suggestions on how to capture a WinForm as a Bitmap to feed to a
> PrintPageEventHandler?
>
> I like this solution much better than using GDI+, which was required in
> Visual Studio 2003.
>
> Thanks,
> --
> Randy
>
>
> protected void PrintActiveForm(object sender, EventArgs e)
> {
> ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;
> //MessageBox.Show(MenuItem.Name);
> PrintDialog PD = new PrintDialog();
> PrinterSettings PS = new PrinterSettings();
> PrintDocument PrtDoc = new PrintDocument();
>
> _ActiveFormImage = SetSeg_Library.SetCapture.Form(this);
>
> PrtDoc.PrintPage += new PrintPageEventHandler(PrtDoc_PrintPage);
> PD.Document = PrtDoc;
> PD.ShowDialog();
> PrtDoc.Print();
>
> }
>
> void PrtDoc_PrintPage(object sender, PrintPageEventArgs e)
> {
> e.Graphics.DrawImage(this._ActiveFormImage, 0, 0);
> }
>


Jun 26 '06 #5
Hi Randy,

Thanks for your feedback!

To draw image while printing or in print preview dialog, you should handle
the print document's PrintPage event, and draw to the Graphic object of
PrintPageEventArgs.

The code snippet below works well on my side:

private void button1_Click(object sender, EventArgs e)
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = new PrintDocument();
ppd.Document.PrintPage += new PrintPageEventHandler(Document_PrintPage);
ppd.ShowDialog();
}

void Document_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bt = new Bitmap(this.dataGridView1.Width,
this.dataGridView1.Height);
this.dataGridView1.DrawToBitmap(bt, new Rectangle(0, 0,
this.dataGridView1.Width, this.dataGridView1.Height));
e.Graphics.DrawImage(bt, new Point(0, 0));
}
Note: I used a already databinding DataGridView control on the form as
printing source, while use a PrintPreviewDialog to show the print result at
runtime. You may give this code sinppet a test on your side. If you need my
sample project please feel free to tell me.

Finally, if you are curious, DrawToBitmap is a .Net2.0 new method, which
encapsulates WM_PRINT Win32 message and BitBlt Win32 API to do the printing
work.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '06 #6
Thanks for all the excellent responses. They helped a lot!
--
Randy
"randy1200" wrote:
I'm using Visual Studio 2005 and C#. I need to print a WinForm used for data
entry as a graphic. In other words, I need to print the exact WinForm the
user sees on the screen.

I searched through the newsgroups, and found the code below. This seems
reasonable, except for the fact that the author does not provide his
SetSeq_Library. This is needed to load the Bitmap variable _ActiveFormImage.

Any suggestions on how to capture a WinForm as a Bitmap to feed to a
PrintPageEventHandler?

I like this solution much better than using GDI+, which was required in
Visual Studio 2003.

Thanks,
--
Randy
protected void PrintActiveForm(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

_ActiveFormImage = SetSeg_Library.SetCapture.Form(this);

PrtDoc.PrintPage += new PrintPageEventHandler(PrtDoc_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();

}

void PrtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(this._ActiveFormImage, 0, 0);
}

Jun 27 '06 #7

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

Similar topics

2
by: Darcy Kahle | last post by:
I am trying to do some advanced printing in python using the win32ui module, and have run into an issue. I need to print a page landscape. As I could not determine how to specify the orientation...
0
by: bozdog | last post by:
Hi I have developed an access 2000 application which utilizes reports with a graphic file as the background of the detail section. There are several subforms placed over the graphic indicating...
0
by: Mark Davison | last post by:
Hi, I hope someone can help, this has been driving me mad! I am trying to generate a graphic on the fly. This is then shown in the browser in a "print preview" type window. The user can click a...
2
by: Philippe | last post by:
I'm writing a vb.net application that prints jpeg pictires, and although I set the defaultpageSettings.printerResolution of my PrintDocument object to the maximum available printer resolution...
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...
4
by: Michele | last post by:
I use the code at the of this message to add a copyright text ( watermark ) on image printing. I would like to have a text Opacity at 30%. On print preview and normal view all is ok , but when I...
9
by: Sukh | last post by:
Hello anyone, I am printing a pre-printed continue paper on dot-matrix printer using vb.net winform. For printing I am creating custom size paper and selecting the same for printing. Everything...
2
by: deepakfordotnet | last post by:
Hi, First of all let me confess that I could not get the solution to the same problem from an earlier post Printing :by Mr.Richard MSL (dated September 24th 2006) working. (Replied by Mr.Walter...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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.