如何在 .NET 中翻转/旋转 PrintDocument?

发布于 2024-07-11 17:47:33 字数 224 浏览 10 评论 0原文

我有一份文档,希望在打印时翻转/旋转 180 度。 (这是由于打印机中标签纸的方向造成的)。

有一个属性 PrintDocument.PrinterSettings.LandscapeAngle 但它是只读的。

我认为该属性受打印机驱动程序的影响,因此不可“设置”。

有没有一种好方法可以让我将打印旋转 180 度而不必做任何太讨厌的事情?

I have a document that I want to be flipped / rotated 180 degrees when printed.
(This is due to the orientation of label stock in the printer).

There is a property PrintDocument.PrinterSettings.LandscapeAngle but it is read only.

I think this property is influenced by the printer driver and therefore not 'settable'.

Is there a nice way i can rotate the print by 180 degrees without having to do anything too nasty?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娇妻 2024-07-18 17:47:33

我想这取决于您对“任何太讨厌的东西”的定义:-)

PrintDocument 类有一个 Graphics 对象来实现此目的,该对象又具有 TranslateTransformRotateTransform 方法可以让您将东西放在您需要的位置。

在操作图形对象之前,通常值得复制它,以便在操作完成后可以再次将其恢复。

I guess that depends on what you define as being "anything too nasty" :-)

The PrintDocument class has a Graphics object you can use for this, which in turn has a TranslateTransform and RotateTransform method that will allow you to get things where you need them to be.

It's often worth taking a copy of the graphics object before you manipulate it so you can restore it back again when you're done.

酒解孤独 2024-07-18 17:47:33

在 VB.NET 中打印表单并翻转/旋转 PrintDocument,并将 DefaultPageSettings 设置为横向

Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
    mPrintBitMap.RotateFlip(RotateFlipType.Rotate90FlipNone)
    mPrintDocument.PrinterSettings.DefaultPageSettings.Landscape = True
    ' Draw the image centered.     
    Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width - mPrintBitMap.Width) \ 2
    Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height - mPrintBitMap.Height) \ 2

    e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)
    ' There's only one page.   
    e.HasMorePages = False
End Sub
Private Sub B_print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_print.Click
    ' Copy the form image into a bitmap.    
    mPrintBitMap = New Bitmap(Me.Width, Me.Height)
    Dim lRect As System.Drawing.Rectangle
    lRect.Width = Me.Width
    lRect.Height = Me.Height
    Me.DrawToBitmap(mPrintBitMap, lRect)
    ' Make a PrintDocument and print.    
    mPrintDocument = New PrintDocument

    mPrintDocument.Print()

End Sub

print a form and flip/rotate a PrintDocument in VB.NET and set DefaultPageSettings to landscape

Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
    mPrintBitMap.RotateFlip(RotateFlipType.Rotate90FlipNone)
    mPrintDocument.PrinterSettings.DefaultPageSettings.Landscape = True
    ' Draw the image centered.     
    Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width - mPrintBitMap.Width) \ 2
    Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height - mPrintBitMap.Height) \ 2

    e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)
    ' There's only one page.   
    e.HasMorePages = False
End Sub
Private Sub B_print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_print.Click
    ' Copy the form image into a bitmap.    
    mPrintBitMap = New Bitmap(Me.Width, Me.Height)
    Dim lRect As System.Drawing.Rectangle
    lRect.Width = Me.Width
    lRect.Height = Me.Height
    Me.DrawToBitmap(mPrintBitMap, lRect)
    ' Make a PrintDocument and print.    
    mPrintDocument = New PrintDocument

    mPrintDocument.Print()

End Sub
痞味浪人 2024-07-18 17:47:33

在将其分配给打印机之前,您是否尝试过 GDI 自行旋转图像? 这就是我所做的:

                _currentPage = Image.FromStream((MemoryStream)_queue.Dequeue());
                pageHeight = _currentPage.Height;
                pageWidth = _currentPage.Width;

                if (pageHeight < pageWidth)
                {
                    _currentPage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    pageHeight = _currentPage.Height;
                    pageWidth = _currentPage.Width;                      

                }

have you tried before assigning it to the printer GDI rotate the image it self? thats what i did:

                _currentPage = Image.FromStream((MemoryStream)_queue.Dequeue());
                pageHeight = _currentPage.Height;
                pageWidth = _currentPage.Width;

                if (pageHeight < pageWidth)
                {
                    _currentPage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    pageHeight = _currentPage.Height;
                    pageWidth = _currentPage.Width;                      

                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文