在页面之间拆分图像进行打印

时间:2014-03-14 12:21:26

标签: vb.net image-processing recursion image-manipulation

我正在尝试在vb.net中打印图像。图像的大小是动态的,并且是从面板派生的,因此可以跨越1页以上。为此,我正在裁剪图像并打印第一部分,然后递归调用该过程来打印下一部分。第一页打印正常,但后续页面是空白的,应该是它们上面的图像 800是页面的高度,1100是宽度。所有保存图像都是为了查明问题:restimg.bmp显示为空白,因此问题似乎出现在第二个using语句中。我对图像处理知之甚少,所以请简单的术语和例子。 这是代码。

Sub recersive_print(ByVal WholeImg As Bitmap)
    If WholeImg.Height > 800 Then
        Dim CropRect As New Rectangle(0, 0, 1100, 800)
        Dim CropImage = New Bitmap(CropRect.Width, CropRect.Height)
        Dim restofimg = New Bitmap(1100, WholeImg.Height - 800)
        Dim restofingrect As New Rectangle(0, 0, restofimg.Height, restofimg.Width)
        Using grp = Graphics.FromImage(CropImage)
            grp.DrawImage(WholeImg, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
        End Using
        CropImage.Save("E:\cropped.bmp")
        Using grp = Graphics.FromImage(restofimg)
            grp.DrawImage(WholeImg, New Rectangle(0, CropRect.Height, restofimg.Width, restofimg.Height), restofingrect, GraphicsUnit.Pixel)
        End Using
        'img_filepath = Application.StartupPath & "\out" & Val(img_filepath) + 1 & ".bmp"
        img_to_print = CropImage
        'CropImage.Save(img_filepath)
        PrintDocument1.Print()
        'WholeImg.Dispose()
        restofimg.Save("E:\Rest.bmp")
        recersive_print(restofimg)

    Else
        img_to_print = WholeImg
        img_to_print.Save("E:\out.bmp")
        PrintDocument1.Print()
    End If
End Sub  

由于

编辑:img_to_print按以下方式使用

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim g As Graphics = e.Graphics
    g.DrawImage(img_to_print, 5, 5)
End Sub

1 个答案:

答案 0 :(得分:0)

800是页面的宽度,1100是高度。我相信你的意思是1100宽度和800高度。

Sub recersive_print(ByVal WholeImg As Bitmap)
    Static i As Integer = 0

    i += 1
    If WholeImg.Height > 800 Then
        Dim CropRect As New Rectangle(0, 0, 1100, 800)
        Dim CropImage = New Bitmap(CropRect.Width, CropRect.Height)
        Dim restofimg = New Bitmap(1100, WholeImg.Height - 800)
        Dim restofingrect As New Rectangle(0, 0, restofimg.Width, restofimg.Height)

        Using grp = Graphics.FromImage(CropImage)
            grp.DrawImage(WholeImg, CropRect, CropRect, GraphicsUnit.Pixel)
        End Using

        CropImage.Save("E:\cropped" & i.ToString ".bmp")

        Using grp = Graphics.FromImage(restofimg)
            grp.DrawImage(WholeImg, restofingrect, New Rectangle(0, CropRect.Height, restofimg.Width, restofimg.Height), GraphicsUnit.Pixel)
        End Using

        restofimg.Save("E:\Rest" & i.ToString ".bmp")
        recersive_print(restofimg)

    Else
        WholeImg.Save("E:\out.bmp")
    End If
End Sub  

瓦尔特