在页面中间打印图片C#

时间:2015-12-18 15:40:45

标签: c# image drawing center

我有一个用c#加载的图像,我想将它以269 x 148毫米的尺寸打印到DIN A4页面,正好在中间。当我通过PDF打印机在文件中打印它时,它完美无缺。但是当我使用我的默认打印机打印它时,左边的marge有点厚,作为正确的marge。

这是我的代码:

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();
    }
}

0 个答案:

没有答案