UWP打印多张图像

时间:2017-04-20 15:04:36

标签: vb.net printing uwp

我修改了Microsoft的打印示例以打印多张图片,但图像始终是纵向对齐的,如何将它们对齐景观?根据微软的说法,图片应该自动旋转。打印示例来自https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Printing

以下是代码:

  ''' <summary>
  ''' Generates a page containing a photo.
  ''' The image will be rotated if detected that there is a gain from that regarding size (try to maximize photo size).
  ''' </summary>
  ''' <param name="photoNumber">The photo number.</param>
  ''' <param name="pageDescription">The description of the printer page.</param>
  ''' <returns>A task that will return the page.</returns>
  Private Async Function GeneratePageAsync(photoNumber As Integer, pageDescription As PageDescription) As Task(Of UIElement)

    Dim page As New Canvas() With {
                .Width = pageDescription.PageSize.Width,
                .Height = pageDescription.PageSize.Height
}

    Dim viewablePage As New Canvas() With {
                .Width = pageDescription.ViewablePageSize.Width,
                .Height = pageDescription.ViewablePageSize.Height
      }

    viewablePage.SetValue(Canvas.LeftProperty, pageDescription.Margin.Width)
    viewablePage.SetValue(Canvas.TopProperty, pageDescription.Margin.Height)

    ' The image "frame" which also acts as a viewport
    Dim blnLandScape As Boolean
    If MainPage._picsperprint = 2 OrElse MainPage._picsperprint = 6 OrElse MainPage._picsperprint = 8 OrElse MainPage._picsperprint = 32 OrElse MainPage._picsperprint = 128 Then
      blnLandScape = True
    ElseIf MainPage._picsperprint > 0 Then
      blnLandScape = False
      'PrintPageSqareNumbers(e, PrintPage)
    ElseIf MainPage._picsperprint = -1 Then
      'PrintPageSized(e, PrintPage)
    End If
    ' Return an async task that will complete when the image is fully loaded.
    Dim blnSuccess As Boolean = False
    Dim pos As Integer = (photoNumber - 1) * MainPage._picsperprint
    For i As Integer = 0 To _Rows - 1
      For ii = 0 To _Columns - 1
        If pos + i * (_Columns) + ii >= MainPage.checkedItems.Count Then Exit For
        Dim height As Integer = pageDescription.PictureViewSize.Height
        Dim width As Integer = pageDescription.PictureViewSize.Width
        height = height / _Rows
        width = width / _Columns
        Dim photoView As New Grid() With {
                .Width = width,
                .Height = height
      }

        ' Center the frame.
        Dim Left As Integer = (viewablePage.Width - photoView.Width * _Columns) / 2
        Dim Top As Integer = (viewablePage.Height - photoView.Height * _Rows) / 2
        Top += height * i
        Left += width * ii
        photoView.SetValue(Canvas.LeftProperty, Left)
        photoView.SetValue(Canvas.TopProperty, Top)
        Dim position As Integer = (pos + i * (_Columns) + ii)
        Dim Item As ImgListItem = GetItemOfPosition(position)
        Dim bitmap As WriteableBitmap = Await Item.LoadBitmapAsync(False)

        If bitmap IsNot Nothing Then
          Dim image As New Image() With {
                    .Source = bitmap,
                    .HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left,
                    .VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top
          }

          ' Use the real image size when croping or if the image is smaller then the target area (prevent a scale-up).
          If photoScale = Scaling.Crop OrElse (bitmap.PixelWidth <= width AndAlso bitmap.PixelHeight <= height) Then
            image.Stretch = Stretch.None
            image.Width = bitmap.PixelWidth

            image.Height = bitmap.PixelHeight

          End If

          ' Add the newly created image to the visual root.
          blnSuccess = True
          photoView.Children.Add(image)
          viewablePage.Children.Add(photoView)
        End If
      Next ii
    Next i
    If blnSuccess Then

      page.Children.Add(viewablePage)


      ' Return the page with the image centered.

      Return page
    Else
      Return Nothing
    End If

  End Function

1 个答案:

答案 0 :(得分:0)

没有自动旋转。必须在Item.LoadBitmapAsync(False)中旋转(转换)图像。