没有预览如何直接将表格打印到打印机

没有预览如何直接将表格打印到打印机

问题描述:

请将代码直接打印到打印机。



谢谢

Surender

Please give the code to print form directly to the printers.

Thanks
Surender

;)


只需更改System.Drawing.Image.FromFile(logomega.png);对于你自己的照片^^



;)
Just change System.Drawing.Image.FromFile("logomega.png"); for a picture of your own ^^

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

namespace OlhaPC
{
    public partial class Principal : Form
    {

        private PrintDocument printDoc = new PrintDocument();

        String osInfo1 = "";
        String osInfo3 = "";
        String osInfo4 = "";
        String osInfo5 = "";
        String osInfo6 = "";
        String osInfo7 = "";
        String osInfo8 = "";
        String osInfo9, osInfo10 = "";
        System.OperatingSystem osInfo2;


        public Principal()
        {
            InitializeComponent();
            printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

            DateTime saveNow = DateTime.Now;

            osInfo2 = System.Environment.OSVersion;
            osInfo1 = System.Environment.MachineName;
            osInfo3 = System.Environment.OSVersion.Platform.ToString();
            osInfo4 = System.Environment.OSVersion.ServicePack.ToString();
            osInfo5 = System.Environment.ProcessorCount.ToString();
            osInfo6 = System.Environment.SystemDirectory.ToString();
            osInfo7 = System.Environment.TickCount.ToString();
            MessageBox.Show(osInfo2.ToString() + Environment.NewLine +
                            osInfo3.ToString() + Environment.NewLine +
                            osInfo4.ToString() + Environment.NewLine +
                            osInfo5.ToString() + Environment.NewLine +
                            osInfo1.ToString() + Environment.NewLine +
                            osInfo7.ToString() + Environment.NewLine +
                            osInfo6.ToString() + Environment.NewLine +
                            saveNow.ToString()
                            );

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        //Impressao (Dados e Tipo de letra etc)
        private void printDoc_PrintPage(Object sender,
           PrintPageEventArgs e)
        {
            
            //System.Drawing.Image img = System.Drawing.Image.FromFile("D:\\Foto.jpg");
            System.Drawing.Image img = System.Drawing.Image.FromFile("logomega.png");
            Point loc = new Point(0, 0);
            e.Graphics.DrawImage(img, loc); 

            String textToPrint = ".NET Printing is easy: ";
            Font printFont1 = new Font("Courier New", 16);
            e.Graphics.DrawString(textToPrint, printFont1,
                  Brushes.Black, 0, 100);

            String textToPrint2 = "So much much easy";
            Font printFont2 = new Font("Courier New", 14);
            e.Graphics.DrawString(textToPrint2, printFont2,
                  Brushes.Red, 310, 101);

            e.Graphics.DrawString(osInfo1, printFont2, Brushes.Red, 20, 141);
            e.Graphics.DrawString(osInfo2.ToString(), printFont2, Brushes.Black, 20, 181);
            e.Graphics.DrawString(osInfo3, printFont2, Brushes.Red, 20, 221);
            e.Graphics.DrawString(osInfo4, printFont2, Brushes.Black, 20, 261);
            e.Graphics.DrawString(osInfo5, printFont2, Brushes.Red, 20, 301);
            e.Graphics.DrawString(osInfo6, printFont2, Brushes.Black, 20, 341);
            e.Graphics.DrawString(osInfo7, printFont2, Brushes.Red, 20, 381);
            //MessageBox.Show(textToPrint.Length.ToString());

        }

        //Butao imprimir com chamada para printDoc.Print
        private void button1_Click(object sender, EventArgs e)
        {
            printDoc.Print();
        }

        
    }
}