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, b开发者_开发百科ut 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?
var img = Image.FromFile("img.jpg");
var w = i.Width / img.HorizontalResolution; //in Inches
var h = i.Height / img.VerticalResolution; //in Inches
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
...
精彩评论