This is a continuation of “Printing multi-frame Tiff images in VB.NET to a certain printer tray.” A problem came up that I hadn’t accounted for and my client came back to me. I was worried I’d have to implement a solution that actually rotated the image, requiring some crazy math voodoo that I prefer to leave to the math to math programmers. Luckily the solution is much more simple.
There’s one problem I found out, you can’t change print settings after you start the printjob in the PrintPage sub, after quite a bit of searching I found that there is another sub QueryPageSettings that allows you to make changes.
Following the previous code we need to make a single change:
-
Private Sub printDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printDoc.PrintPage
-
tiffImage.SelectActiveFrame(FrameDimension.Page, start - 1) ‘<– comment this out
-
start += skip
-
e.Graphics.DrawImage(tiffImage, 0, 0)
-
If (start < tiffImage.GetFrameCount(FrameDimension.Page)) Then
-
e.HasMorePages = True
-
Else
-
e.HasMorePages = False
-
End If
-
End Sub
And then we need add the QueryPageSettings sub, it’s actually another event of the printdocument so in the IDE you can select the event instead.
-
Private Sub printDoc_QueryPageSettings(ByVal sender As Object, ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles printDoc.QueryPageSettings
-
tiffImage.SelectActiveFrame(FrameDimension.Page, start - 1)
-
Console.Write(String.Format("Frame Dimensions: {0} x {1} ", tiffImage.Width, tiffImage.Height))
-
If tiffImage.Width > tiffImage.Height Then
-
e.PageSettings.Landscape = True
-
Console.Write("Orientation: Landscape ")
-
Else
-
e.PageSettings.Landscape = False
-
Console.Write("Orientation: Portrait ")
-
End If
-
End Sub
As you can see that’s all there is to it, you compare the width and height of the image and if the width is wider than the height you enable landscape orientation in the pageSettings. The reason we commented out the line in the PrintPage sub was because we are now setting the active frame in the new sub. QueryPageSettings is raised before every PrintPage event so you have time to make adjustments to your print settings on the fly.
One issue I noticed, however, is that the image gets offset by a small amount (about 20-30px) and parts of the image can get cut off. As a result I’ve also done some calculations to resize the images width and height to fit the page and the following line:
-
e.Graphics.DrawImage(tiffImage, 0, 0)
Needs to be changed to:
-
e.Graphics.DrawImage(tiffImage, offsetX, offsetY, width, height)
The width and height integers are in hundredths of inches, so an 8.5″ x 11″ sheet is represented by width = 850 and height = 1100. There’s some math involved to figure out the ratio to scale the image to the size of paper you’re printing to, but since I’m using only 8.5″ x 11″ I just forced the variables to be 830 x 1070 (or 1070 x 830 if landscape,) it’s arbitrary but my program is just a stop gap until they get a more permanent solution in place.
Technorati Tags: VB.NET, Multi-Frame Tiff, Printing, Page Orientation
Popularity: 100% [?]