2

i have a Image loaded in c# and i would like to print it in the size of 269 x 148 mm to a DIN A4 page in landscape exactly in the middle. When i print it via PDF Printer in a file, it works perfect. But when i print it with my Default Printer, the left marge is a Little bit thicker as the right marge.

Here´s my Code:

private void PrintImage(Image img) {
    PrintDocument pd = new PrintDocument();

    //Disable the printing document pop-up dialog shown during printing.
    PrintController printController = new StandardPrintController();
    pd.PrintController = printController;

    pd.DefaultPageSettings.Landscape = true;
    pd.OriginAtMargins = false;

    pd.PrintPage += (sndr, args) => {
        System.Drawing.Image i = img;

        //Adjust the size of the image to the page to print the full image without loosing any part of the image.
        System.Drawing.Rectangle m = args.MarginBounds;
        Rectangle t = args.MarginBounds;

        //PrintDialog myPrintDialog1 = new PrintDialog();

        //Logic below maintains Aspect Ratio.
        double cmToUnits = 100 / 2.54;
        m.Width = (Int32)(width * cmToUnits);
        m.Height = (Int32)(height * cmToUnits);

        pd.DefaultPageSettings.Landscape = true;
        //Putting image in center of page.
        m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Height) / 2);
        m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Width) / 2);
        args.Graphics.DrawImage(i, m);    
    };

    PrintDialog myPrintDialog1 = new PrintDialog();
    myPrintDialog1.Document = pd;
    if (myPrintDialog1.ShowDialog() == DialogResult.OK){
        pd.Print();
    }
}
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Christian
  • 75
  • 10
  • It is quite normal for different printers to have different margins, if that's what the issue is. But what I believe you meant is printable area, not actual margins which are defined by your code. – bokibeg Dec 18 '15 at 15:46
  • I had another look at your code, it looks to me like you want to use either `args.PageBounds` or `args.PageSettings.PrintableArea`, definitely *not* margins which are not centered on many printers. I can explain these in detail if that helps you solve the problem. – bokibeg Dec 18 '15 at 15:59
  • I find out that the DefaultPageSetting.PrintableAreas are different as the PageSize is. I changed the two lines to "printableArea": `m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PrintableArea.Width - m.Height) / 2); m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PrintableArea.Height - m.Width) / 2);` – Christian Dec 18 '15 at 16:09
  • So it´s working now with `PrintableArea` thank you – Christian Dec 18 '15 at 16:18

0 Answers0