Quantcast
Viewing all articles
Browse latest Browse all 1449

VB6 - Print StdPicture Objects

I'm sure this has been covered lots of times already, but here's another take.

The basic idea is to use the Printer object's PaintPicture method to crop and scale a StdPicture as you print it. The only wrinkle here is making a few API calls to the spooler to get lists of paper sizes and printer resolutions. The user picks those things and thenwe have the metrics we need to calculate our cropping and scaling.

This demo uses a fixed StdPicture that it loads from a sample image file included in the attached archive. You could just as easily use StdPictures from a PictureBox, a WIA object, or from somewhere else. It scales the picture to fit centered within the bounds of some fixed-size margins within a chosen paper size and orientation:

Image may be NSFW.
Clik here to view.
Name:  Illustration.png
Views: 64
Size:  131.0 KB

You could add cropping, you could use user-chosen margins, you could fit the image into some smaller region of the paper, etc. Here I just show how you'd go about it. To make those changes you'd just modify the calculations to fit your needs.

Code:

Private Sub cmdPrint_Click()
    'Print the StdPicture Pic centered on the selected rrinter (Pr)
    'with the selected paper (lstPapers) at the selected quality
    '(lngResolutions) within set margins.
    Dim MarginsLR As Single
    Dim MarginsTB As Single
    Dim PrintableWidth As Single
    Dim PrintableHeight As Single
    Dim ScaleFactor As Double
    Dim ScaledWidth As Double
    Dim ScaledHeight As Double

    Set Printer = Pr
    With Printer
        'Set up paper:
        .PaperSize = intPaperIds(lstPapers.ListIndex)
        .PrintQuality = lngResolutions(2 * lstResolutions.ListIndex) 'We can only set one
                                                                    'value DPI value, just
                                                                    'use X here.
        If optOrientation(1).Value Then
            .Orientation = vbPRORLandscape
        Else
            .Orientation = vbPRORPortrait
        End If

        'Scale to paper, using 0.5" margins all around.  Could also crop
        'the image here:
        MarginsLR = .ScaleX(0.5, vbInches, .ScaleMode)
        MarginsTB = .ScaleY(0.5, vbInches, .ScaleMode)
        PrintableWidth = .Width - 2 * MarginsLR
        PrintableHeight = .Height - 2 * MarginsTB

        ScaleFactor = PrintableWidth / .ScaleX(Pic.Width, vbHimetric, .ScaleMode)
        If ScaleFactor * .ScaleY(Pic.Height, vbHimetric, .ScaleMode) > PrintableHeight Then
            ScaleFactor = PrintableHeight / .ScaleY(Pic.Height, vbHimetric, .ScaleMode)
        End If

        ScaledWidth = ScaleFactor * .ScaleX(Pic.Width, vbHimetric, .ScaleMode)
        ScaledHeight = ScaleFactor * .ScaleY(Pic.Height, vbHimetric, .ScaleMode)

        'Paint (print) the image, scaled.  Could also do the actual cropping
        'here if any were desired by specifying additional arguments:
        .PaintPicture Pic, _
                      (.Width - ScaledWidth) / 2, _
                      (.Height - ScaledHeight) / 2, _
                      ScaledWidth, _
                      ScaledHeight
        .NewPage
        .EndDoc
    End With
End Sub

The demo application looks like this:

Image may be NSFW.
Clik here to view.
Name:  sshot.png
Views: 66
Size:  29.4 KB

You could show the paper sizes in mm or inches instead of 1/10 mm by doing the calculations. Those are just the units returned by the print spooler so to keep code complexity down I used them as is.

The code involved is fairly brief and the Project files are small. The attached archive is only so large because of the included sample image.
Attached Images
Image may be NSFW.
Clik here to view.
 Image may be NSFW.
Clik here to view.
 
Attached Files

Viewing all articles
Browse latest Browse all 1449

Trending Articles