yushff

code the world。

导航

解读PrintDocument 的PrintPage事件

Posted on 2010-07-24 14:19  yushff  阅读(1365)  评论(0编辑  收藏  举报

解读PrintDocument 的PrintPage事件

   PrintDocument 的PrintPage事件,他的执行方式是当 e.HasMorePages = True就执行一次PrintDocument 的PrintPage事件,直到e.HasMorePages = False时,才停止。

下面是打印多个图片的代码:

 

Private Sub PrintGraphicControl_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintGraphicControl.PrintPage
        Try
            'Dim XBegin As Integer
            'Dim YBegin As Integer
            'PicShow.fittoscreen()
            Dim ScaleParameter As Double '有可能放大也可能缩小
            Dim ImageInchCount As Double '图像英寸的数量
            Dim PaperInchCount As Double '图纸英寸的数量
            Dim Tmpimg As Image
        
            If StrFileName.Length - 1 = LstPicView.Items.Count Then
                Tmpimg = Image.FromFile(StrFileName(CurrentPageNumber).Trim)
                '当图像的宽度大于高度时,对图像进行旋转处理
                If Tmpimg.Size.Width > Tmpimg.Size.Height Then
                    Tmpimg.RotateFlip(RotateFlipType.Rotate90FlipNone)
                End If

                '图像的高度/宽度比 >  图纸的高度/宽度比 时
                '以高度填充整个图纸
                If (Tmpimg.Size.Height / Tmpimg.Size.Width) > (e.PageSettings.PaperSize.Height / e.PageSettings.PaperSize.Width) Then
                    ImageInchCount = Tmpimg.Size.Height / 72 '英寸数
                    PaperInchCount = e.PageSettings.PaperSize.Height / 100 '英寸数
                    ScaleParameter = PaperInchCount / ImageInchCount
                    '图像的高度/宽度比  <=  图纸的高度/宽度比 时
                    '以宽度填充整个图纸
                Else
                    ImageInchCount = Tmpimg.Size.Width / 72
                    PaperInchCount = e.PageSettings.PaperSize.Width / 100
                    '当图像英寸的高度大于纸张英寸的高度时
                    ScaleParameter = PaperInchCount / ImageInchCount
                End If

                Dim imgnew As New System.Drawing.Bitmap(Tmpimg, CInt(Tmpimg.Size.Width * ScaleParameter), CInt(Tmpimg.Size.Height * ScaleParameter)) '新建一个放大的图片

                e.Graphics.DrawImage(imgnew, e.MarginBounds)
                'e.Graphics.DrawImage(imgnew, e.Graphics.VisibleClipBounds)
                CurrentPageNumber = CurrentPageNumber + 1
                If CurrentPageNumber <= PageCount Then
                    e.HasMorePages = True
                Else
                    e.HasMorePages = False
                End If
           
            End If
        Catch ex As Exception
        End Try
    End Sub