0

I am trying to print an image in vb.net. The size of the image is dynamic, and is derived from a panel, so can span more than 1 page. For this, I am cropping the image and printing the first part, then recursively calling the procedure to print the next section. The first page prints okay, but the subsequent pages are blank, as is the image that is supposed to be on them.
800 is height of page, 1100 is width. All the save images are to pinpoint the problem: restimg.bmp comes up as blank, so the problem seems to be in the second using statement. I know very little about image manipulation, so simple terms and example please. This is the code.

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  

Thanks

EDIT: img_to_print is used in the following way

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
jsd150
  • 3
  • 4

1 Answers1

0

800 is width of page, 1100 is height. i believe you mean 1100 width and 800 height.

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  

valter

  • Doing the first bit mucked up the first page, and none of it made any of it appear on the subsequent pages. The saving of the images was just to allow me to see what was in each image at what step, they will not be a part of the finished program. The height and width were the wrong way around, changing it now. – jsd150 Mar 16 '14 at 13:13
  • @jsd150 Is wrong to have `img_to_print` in the recursive function as you are setting to it images with different sizes eg `img_to_print = CropImage and img_to_print = WholeImg`. – γηράσκω δ' αεί πολλά διδασκόμε Mar 16 '14 at 13:48
  • img_to_print is a global variable, that is used in PrintDocument1_PrintPage(). see edit above. – jsd150 Mar 16 '14 at 14:42
  • Changing `restofingrect` makes No difference to the first page, regardless of if I try it with either `grp.DrawImage`. Still nothing on other pages. – jsd150 Mar 16 '14 at 15:08
  • @jsd150 Does the bitmaps `cropped.bmp, Rest.bmp, out.bmp`apear correctly? You are using img_to_print as a pointer to different bitmaps. Thats is ok. Not clone is needed. – γηράσκω δ' αεί πολλά διδασκόμε Mar 16 '14 at 15:18
  • After the first loop, cropped is ok, rest is blank. After that, All are blank. I have pictures at [google site](https://sites.google.com/site/forumstuffjd150/) – jsd150 Mar 16 '14 at 15:32
  • @jsd150 There is a problem as i mentioned(save location) because your function is called 3 times (your image is 1100x1699). If you decrease the image size to 1100x1600 it will run twice. Do that and remove also `PrintDocument1.Print()` call, just for testing. I have tested it and its ok. – γηράσκω δ' αεί πολλά διδασκόμε Mar 16 '14 at 15:47