none
Printing to printer

    Question

  • I'm extremely new to visual language, and have not had the chance to take the beginner courses, so please bare with me.

    After getting help from various places, and much trial and error, I have worked out all of the errors that Visual Studio screams at me. The issue I'm currently working through is that I only get blank pages when printing from the debugger. What am I doing wrong, and where can I go to get a better understanding of the problem?

    P.S.

    I'm attempting to print a string that was passed from another form.

    Code follows:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Printing;

    namespace WindowsFormsApplication1
    {

       
        public partial class Print : Form
        {

            public Print(string report)
            {
                InitializeComponent();
                lblReport.Text = report; 
            }
            private void btnPrint_Click(object sender, EventArgs e)
            {
                string s = lblReport.Text;

                PrintDocument p = new PrintDocument();
                p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
                {
                    e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));

                };
                try
                {
                    p.Print();
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception Occured While Printing", ex);
                }
            }

            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(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {

            }

            private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e)
            {
                e.Graphics.DrawImage(memoryImage, 0, 0);
            }
            private void btnCancel_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
        }

    Sunday, April 2, 2017 4:54 PM

All replies