简体   繁体   中英

C#, how to get paper size in pixels?

I'm trying to print a table stored in a BMP image. I don't know in advance the table size, paper size or printer resolution. While the table fits into 1 page, everything is fine, but when it grows larger then the sheet of paper, c# just cuts off the reminder of the table.

I understand that I have to split the image into several smaller ones manually and print each one on a separate page, but I'm having problems on deciding where to split. My problem is that I can get table dimensions in pixels but the sheet size is in inches, so I have no idea how much of the table can fit on one sheet. How can I get both values in same units?

This is typically done with the System.Drawing.Printing.PrintPageEventArgs class.

Since you can select different Printers with different DPI resolutions, the pixel size will vary. Therefore this property is best read out through events.

You need to initiate the printing like this:

PrintDocument _printDocument = new PrintDocument();
_printDocument.PrintPage += new PrintPageEventHandler(_printDocument_PrintPage);
...
void _printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
int pageWidth = e.PageBounds.Width
...
var img = Image.FromFile("img.jpg");
var w = i.Width / img.HorizontalResolution; //in Inches
var h = i.Height / img.VerticalResolution;  //in Inches

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM