2017-04-20 58 views
0

我已經修改了微軟的打印示例,用於打印多張照片,但圖像始終是對齊的人像,如何將它們對齊到橫向?根據微軟的照片應該自動旋轉。打印示例從https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/PrintingUWP打印多張圖片

下面是代碼:

''' <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 
+0

什麼是您正在使用的打印示例?你在哪裏看到圖片應該自動旋轉? – Scavenger

+0

該代碼來自https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Printing,並在評論中說'''如果檢測到圖像有增益,圖像將被旋轉關於尺寸(嘗試最大化照片尺寸)。 –

回答

0

沒有自動旋轉。圖像必須在Item.LoadBitmapAsync(False)中旋轉(變形)。