Prints topics for C #

Daily water at the beginning, looked under the last blog, and eight days a year, tut, luxury.

Recently the job quite satisfied, I want direction, subsequent machine learning to do, have to accumulate before the first company to solve the problem now.

Talk about life is over, or that the main topic. (I feel this title can not attract people ah)

 

 

Print: PrintDocument

Environment: WPF and Winform

Requirements: typesetting and printing pictures

Before parameters did not understand a lot of problems, and now want to clear, readily record it.

WPF temporarily put aside, to be resolved before the winform, with Baidu bad street of the same event is PrintPage Graphics drawing to print. Other part of the reference

https://www.cnblogs.com/hsiang/p/6921817.html

First with Graphics draw I did not understand is how to draw, how much is the size of the paper, how to print the specified size.

// Let's talk about paper size, see the size of the paper can be printed at the beginning of the file
  PrintDocument.Print(); 
And after the selected printer, drawing data to be printed in an event where PrintPage
private void pdControl_PrintPage(object sender, PrintPageEventArgs e)
{
    var printDocument = (PrintDocument)sender;
    //纸张的高和宽
    var paper_h = printDocument.DefaultPageSettings.PaperSize.Height;
    var paper_w = printDocument.DefaultPageSettings.PaperSize.Width;
}

But know but do not know the height and width of the unit, ah, this height and width in pixels or inches in the end or the other what the hell.

In the example of the new PaperSize to see Note:

Hundredths of an inch

 The first such problem is solved, the paper size is printDocument.DefaultPageSettings.PaperSize, and the units are hundredths of an inch.

But do not be too sure of this, for example, I want to print a picture is 6 inches (specifically the amount), but given the size width and more 0.15In, much higher 0.13In. Anyhow win comes with PDF printing A4 paper will differ by 0.002 or less okay. Why does this have to be the god to explain the error.

 

Then the second question (third): how to draw in order to print the specified size

Now that we know the scope of it to avoid drawing print beyond the paper. Then we will draw content, and nothing if they talk about the picture, the other almost.

What I do is based on the size of the picture covered the entire paper. 

   // Let's build a class, and to record the image size to the paper size
// This class is only to facilitate debugging convert units of measure, the practical application only choose a unit of the line class PrintUnit {
     // size of the picture, where the picture is actually an inch photos
public int unit_w = 259 ; public int unit_h = 377 ;
     // paper size. 4 * 300 *. 6 300dpi 300
public the PaperSize = the paperSize new new the PaperSize ( " A6 " , 1800 , 1200 );
     // metric
Private the GraphicsUnit unit = the GraphicsUnit .pixel; public GET GraphicsUnit Unit { => Unit; SET { Unit = value;
          // if metric millimeters
IF (Unit == GraphicsUnit.Millimeter) {
            // particularly in terms of 1200 / 25.4 is approximately equal to 300 * 102
  paperSize.Height
= 102 ; paperSize.Width = 152 ;
unit_w
= 22 is ; unit_h = 32 ; } } } }

 

Then PrintPage event where the paper can be calculated how much covered with pictures

            PrintUnit printUnit = new PrintUnit();
       g.PageUnit = printUnit.Unit;
       column = printUnit.paperSize.Width / printUnit.unit_w; row = printUnit.paperSize.Height / printUnit.unit_h; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { //g.DrawImage( // Resources._1, // new Rectangle(j * printUnit.unit_w, i * printUnit.unit_h, printUnit.unit_w, printUnit.unit_h), // new Rectangle(0, 0, Resources._1.Width, Resources._1.Height), // g.PageUnit); g.DrawImage(Resources._1, j * printUnit.unit_w, i * printUnit.unit_h, printUnit.unit_w, printUnit.unit_h); } }

  Interestingly in DrawImage (Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit); overloads in the picture there is an error, for unknown reasons.

 A bit chaotic, I forced to explain to a wave.

In the event where PrintPageEventArgs.PageUnit print is set up the unit of measure is the unit you want to draw a pixel or a millimeter or in accordance with other default is hundredths of an inch.

// For example, you want to print on A4 paper 210mm × 297mm, print PrintPage event in the set unit of measurement Millimeter 
g.PageUnit = GraphicsUnit.Millimeter;
// covered the entire A4 paper
g.DrawImage (image, 0, 0, 210, 297) ;

 It is that simple, but is not recommended for use in millimeters, because it is easy to picture errors because millimeters will be converted into pixels when drawing.

Obviously there will be a gap when plotted in millimeters

 

 Too lazy to typesetting, something more, a slip of the slip.

 

 

 

 In front of the high-energy remind code mess, you're welcome.

 https://files.cnblogs.com/files/zlyxm/DemoPrint_zlyxm.rar

  

 

 

Guess you like

Origin www.cnblogs.com/zlyxm/p/10937940.html