Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Screen Print
 

Screen Print

This seems to be a very simple thing but I just can't find anything to tell me how to do this. Everything seems to point to printing a document. I just want to print a hard copy of the current (active)screen (window/form).

Thanks for any help, JasinB(David)

jasinb  Wednesday, August 09, 2006 6:53 PM

Try something like this.

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

{

Bitmap bm = new Bitmap(this.Width, this.Height);

this.DrawToBitmap(bm, this.ClientRectangle);

e.Graphics.DrawImage(bm, 0, 0);

}

}

Ken Tucker  Wednesday, August 09, 2006 10:41 PM

Try something like this.

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

{

Bitmap bm = new Bitmap(this.Width, this.Height);

this.DrawToBitmap(bm, this.ClientRectangle);

e.Graphics.DrawImage(bm, 0, 0);

}

}

Ken Tucker  Wednesday, August 09, 2006 10:41 PM

Hi Ken,

Can you resend as Visual Basic code? I saw something like yours but was a lot more complicated.

Thanks again, JasinB(David)

jasinb  Thursday, August 10, 2006 5:55 PM

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

Dim w As Integer = Me.Width

Dim h As Integer = Me.Height

Dim bm As New Bitmap(w, h)

Me.DrawToBitmap(bm, Me.ClientRectangle)

e.Graphics.DrawImage(bm, 0, 0)

End Sub

Ken Tucker  Thursday, August 10, 2006 11:10 PM

Can you resend as Visual c++ code please?

Thank you!

Eduardo A. Carro  Saturday, August 12, 2006 12:12 AM
private: System::Void printDocument1_PrintPage(System::Object^ sender, System::Drawing::Printing::PrintPageEventArgs^ e)
{
Bitmap^ bm = gcnew Bitmap(this->Width, this->Height);
this->DrawToBitmap(bm, this->ClientRectangle);
e->Graphics->DrawImage(bm, 0, 0);
}
Ken Tucker  Saturday, August 12, 2006 12:51 AM
Thanks a lot Ken!
Eduardo A. Carro  Sunday, August 13, 2006 9:59 PM

HI Ken,

Thanks. But for some reason, it almost works. It cuts off the bottom of the form it's printing.

I did finally find other code that did the job. The first line of codeis placed just below Public Class Form 1

Dim memoryImage As Bitmap

Private Sub CaptureScreen()

Dim myGraphics As Graphics = Me.CreateGraphics()

Dim s As Size = Me.Size

memoryImage = New Bitmap(s.Width, s.Height, myGraphics)

Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)

memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _

ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

e.Graphics.DrawImage(memoryImage, 35, 25)

End Sub

I run this from a button with these two line

CaptureScreen()

PrintDocument1.Print()

Note the 35 & 25 in the DrawImage method sets the upper left corner so that the form is centered on the piece of paper. Of course the numbers will need to be readjusted for different sized forms.

Thanks Again, JasinB(David)

jasinb  Monday, August 14, 2006 7:19 PM
From the code:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
       Bitmap bm = new Bitmap(this.Width, this.Height);
       this.DrawToBitmap(bm, this.ClientRectangle);
       e.Graphics.DrawImage(bm, 0, 0);
}


I feel better to use the following statement: To get Clear and Exact Window size Picture

this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));

instead of this statement

this.DrawToBitmap(bmp, this.ClientRectangle);


My Best Solutions  Sunday, August 23, 2009 8:10 AM
I got Problem at once. And i resolved that Problem.
Try this;

public partial class Form3 : Form
    {
        PrintDocument PrintDoc1 = new PrintDocument();
        PrintPreviewDialog PPDlg1 = new PrintPreviewDialog();
        
        public Form3()
        {
            InitializeComponent();

            PPDlg1.Document = PrintDoc1;
            PrintDoc1.OriginAtMargins =true; //To set or Get the Position of a Graphic Object
            PrintDoc1.PrintPage += PDoc_PrintPage;
        }          
        
        private void btn_PrintPreview_Click(object sender, EventArgs e)
        {// When PrintPreview Button Clicks
            PPDlg1.ShowDialog();            
        }

        private void PDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Bitmap bmp = new Bitmap(this.Width, this.Height);
            //this.DrawToBitmap(bmp, this.ClientRectangle);
            this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); //Takes the Snap of the Exact WindowForm size as Bitmap image
            e.Graphics.DrawImage(bmp, 0, 0);
        }

        private void btn_PrintPage_Click(object sender, EventArgs e)
        {//When Print Button Clicks, Image will be show & Ready to Print
            PrintDoc1.Print();
        }

    }

My Best Solutions  Sunday, August 23, 2009 8:21 AM

You can use google to search for other answers

Custom Search

More Threads

• OleDbCommand insert command ends with bool value
• How to handle tic marks ' ' in data when running SQL queries in C#
• Syntax error in INSERT INTO statement in vb2008 express and 2007accdb
• Using the data binding events to disable some rows in a DataGridView
• Can anyone tell me what logic can i apply in the following situation? (Parsing)
• Customize Column/Cell Width in Datagrid in Visual C# 2003 .NET?
• "ListManager's position must be equal to rowNum"
• How to add radio button to DataGridView
• ComboBox, DataBindings with Text property
• DataGridViewComboBox binding isues