C# 在页的中间打印图片 我有一个在C中加载的图像,我想把它打印在269×148毫米的大小到DIA4页面中,正好在中间。当我通过PDF打印机将其打印到文件中时,效果非常好。但是,当我用默认打印机打印时,左边缘比右边缘厚一点

C# 在页的中间打印图片 我有一个在C中加载的图像,我想把它打印在269×148毫米的大小到DIA4页面中,正好在中间。当我通过PDF打印机将其打印到文件中时,效果非常好。但是,当我用默认打印机打印时,左边缘比右边缘厚一点,c#,image,drawing,center,C#,Image,Drawing,Center,这是我的代码: private void PrintImage(Image img) { PrintDocument pd = new PrintDocument(); //Disable the printing document pop-up dialog shown during printing. PrintController printController = new StandardPrintController(); pd.PrintContro

这是我的代码:

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

不同的打印机有不同的页边距是很正常的,如果这就是问题所在的话。但我相信你的意思是可打印区域,而不是由代码定义的实际边距。我又看了一遍你的代码,在我看来你想使用
args.PageBounds
args.PageSettings.PrintableArea
,绝对不是许多打印机上没有居中的边距。如果这有助于解决问题,我可以详细解释这些。我发现DefaultPageSetting.PrintableAreas与PageSize不同。我将这两行更改为“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)所以它现在正在使用
可打印区域
谢谢