Results 1 to 2 of 2

Thread: VB6 - Print StdPicture Objects

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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:

    Name:  Illustration.png
Views: 5189
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:

    Name:  sshot.png
Views: 5162
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 Files Attached Files
    Last edited by dilettante; Jun 19th, 2013 at 06:40 PM.

  2. #2
    New Member
    Join Date
    Jun 2013
    Posts
    5

    Re: VB6 - Print StdPicture Objects

    Well, thanks for sharing!! It is a great job

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width