Buck Hicks
Buck Hicks

Reputation: 1574

I need help converting System.Window.Size to System.Window.Point

I just started working on an existing VB project where the end user wants the ability to print a WPF window as a full page to a printer. I found this code sample in C# and it worked just fine in C#

Printing WPF Window to Printer and Fit on a Page

However when I tried converting it to VB I am getting two errors

  1. System.Drawing.Size cannot be converted to System.Windows.Size
  2. System.Drawing.Point cannot be converted to System.Windows.Point

I sort of know what the difference in Drawing.Size and Windows.Size is based on this (and a couple of other) SO threads What is the difference between System.Drawing.Point and System.Windows.Point? but I cannot figure out how to modify my translation in a way that I can make the conversion happen in VB the way it works on C#. The errors appear on the Measure(sz) and the Arrange(new Rect(.....) lines.

What do I need to do to make this work?

Private Sub PrintWindow()
    Dim printDlg As PrintDialog = New PrintDialog()

    If printDlg.ShowDialog() = True Then

        Dim capabilities As System.Printing.PrintCapabilities =
            printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket)

        Dim scale As Double = Math.Min(capabilities.PageImageableArea.ExtentWidth / ActualWidth,
                                       capabilities.PageImageableArea.ExtentHeight / ActualHeight)

        LayoutTransform = New ScaleTransform(scale, scale)

        Dim sz As New Size(CInt(capabilities.PageImageableArea.ExtentWidth),
                           CInt(capabilities.PageImageableArea.ExtentHeight))

        Measure(sz)

        Arrange(New Rect(New Point(CInt(capabilities.PageImageableArea.OriginWidth),
                                   CInt(capabilities.PageImageableArea.OriginHeight)), sz))

        printDlg.PrintVisual(Me, "First Fit to Page WPF Print")
    End If
End Sub

Upvotes: 0

Views: 861

Answers (1)

eran otzap
eran otzap

Reputation: 12533

Why not do something like this ?

    Dim sz As New System.Windows.Size(CInt(capabilities.PageImageableArea.ExtentWidth),
                       CInt(capabilities.PageImageableArea.ExtentHeight))


    Arrange(New Rect(New System.Windows.Point(CInt(capabilities.PageImageableArea.OriginWidth),
                               CInt(capabilities.PageImageableArea.OriginHeight)), sz))

Upvotes: 1

Related Questions