C# 从PrintPreview创建的图形是纵向的而不是横向的?

C# 从PrintPreview创建的图形是纵向的而不是横向的?,c#,winforms,print-preview,C#,Winforms,Print Preview,我用C语言打印自定义页面。当实际打印文档时,它工作正常,通过相同的代码将其显示到对话框中。当代码用于打印预览时,对话框以横向模式显示页面,但创建的图形具有纵向文档的尺寸,因此,预览无法正确显示。下面是我正在使用的代码的简化版本 using (PrintDocument pd = new PrintDocument()) { pd.PrinterSettings.PrintToFile = false; pd.DefaultPageSettings.Landscape = true

我用C语言打印自定义页面。当实际打印文档时,它工作正常,通过相同的代码将其显示到对话框中。当代码用于打印预览时,对话框以横向模式显示页面,但创建的图形具有纵向文档的尺寸,因此,预览无法正确显示。下面是我正在使用的代码的简化版本

using (PrintDocument pd = new PrintDocument())
{
    pd.PrinterSettings.PrintToFile = false;
    pd.DefaultPageSettings.Landscape = true;
    pd.PrinterSettings.DefaultPageSettings.Landscape = true;
    pd.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Landscape = true;

    PrintDialog pDialog = new PrintDialog();
    pDialog.Document = pd;
    pDialog.PrinterSettings.DefaultPageSettings.Landscape = true;
    pDialog.PrinterSettings.PrintToFile = false;
    pDialog.Document.DefaultPageSettings.Landscape = true;

    PrintPreviewDialog printPreview = new PrintPreviewDialog();

    printPreview.Document = pd;
    printPreview.ShowDialog();
}
然后,当PrintPreview对话框请求打印时,调用Print_Me函数:

private void Print_Me(object sender, PrintPageEventArgs e)
{
    using (Graphics g = e.Graphics)
    {    
        DrawToDC(g);
        e.HasMorePages = hasMorePages;
    }
}
在DrawToDC中,我使用以下方法获得尺寸,正如我所提到的,这些尺寸可以很好地用于真正的打印和显示到对话框中:

dc.VisibleClipBounds.Width
dc.VisibleClipBounds.Height

我有完全相同的问题,最终发现了这个。添加OnQueryPageSettings委托处理程序

void OnQueryPageSettings(object obj,QueryPageSettingsEventArgs e)
{
    if (e.PageSettings.PrinterSettings.LandscapeAngle != 0)
        e.PageSettings.Landscape = true;            
}
和你的打印文档

prnDoc.QueryPageSettings+=新的QueryPageSettingsEventHandlerOnQueryPageSettings


这为我解决了问题。

我也遇到了同样的问题,最终发现了这个问题。添加OnQueryPageSettings委托处理程序

void OnQueryPageSettings(object obj,QueryPageSettingsEventArgs e)
{
    if (e.PageSettings.PrinterSettings.LandscapeAngle != 0)
        e.PageSettings.Landscape = true;            
}
和你的打印文档

prnDoc.QueryPageSettings+=新的QueryPageSettingsEventHandlerOnQueryPageSettings


这为我解决了问题。

我找不到在哪里连接大卫·博尔顿的解决方案,但找到了另一种方法


基本上,您需要在DocumentPaginator的GetPage方法返回的每个DocumentPage上设置页面大小。

我找不到在哪里连接David Bolton的解决方案,但找到了另一种方法


基本上,您需要在DocumentPaginator的GetPage方法返回的每个DocumentPage上设置页面大小。

我遇到了完全相同的问题。但是,如果我以正确的宽度和高度绘制页面内容,即交换页面内容,则一切正常

int width = dc.VisibleClipBounds.Width;
int height = dc.VisibleClipBounds.Height;
if(width < height)
{
    int temp = width;
    width = height;
    height = temp;
}
然后根据宽度和高度绘制页面内容


这不是最好的解决方案,但可以确保我们始终绘制到横向页面。

我也遇到了同样的问题。但是,如果我以正确的宽度和高度绘制页面内容,即交换页面内容,则一切正常

int width = dc.VisibleClipBounds.Width;
int height = dc.VisibleClipBounds.Height;
if(width < height)
{
    int temp = width;
    width = height;
    height = temp;
}
然后根据宽度和高度绘制页面内容

这不是最好的解决方案,但可以确保我们始终绘制到横向页面