Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Printer and PictureBox question

Posted on 2005-04-09
5
Medium Priority
?
772 Views
Last Modified: 2008-01-09
Hi

I am trying to write some text to a PictureBox and Printer object using the same X, Y coordinates and Picture. In otherwords, the Printed page should be identical to the PictureBox. When I use this code it works great:

Private mRec As RECT
Private Const mText = "THIS IS A LONG SENTENCE THAT WILL BE BROKEN DOWN INTO SEVERAL LINES"

Private Sub Command1_Click()
    Set Picture1.Picture = LoadPicture(App.Path & "\PORTRAIT.BMP")
    Picture1.CurrentX = 1600
    Picture1.CurrentY = 1000
    mRec.Left = Picture1.ScaleX(1600, Picture1.ScaleMode, vbPixels)
    mRec.Right = mRec.Left + Picture1.ScaleX(3000, Picture1.ScaleMode, vbPixels)
    mRec.Top = Picture1.ScaleY(1000, Picture1.ScaleMode, vbPixels)
    mRec.Bottom = mRec.Top + Picture1.ScaleY(3000, Picture1.ScaleMode, vbPixels)
    Call DrawText(Picture1.hDC, mText, -1, mRec, DT_WORDBREAK)

    Call Printer.PaintPicture(LoadPicture(App.Path & "\PORTRAIT.BMP"), 0, 0)
    Printer.CurrentX = 1600
    Printer.CurrentY = 1000
    mRec.Left = Printer.ScaleX(1600, Printer.ScaleMode, vbPixels)
    mRec.Right = mRec.Left + Printer.ScaleX(3000, Printer.ScaleMode, vbPixels)
    mRec.Top = Printer.ScaleY(1000, Printer.ScaleMode, vbPixels)
    mRec.Bottom = mRec.Top + Printer.ScaleY(3000, Printer.ScaleMode, vbPixels)
    Call DrawText(Printer.hDC, mText, -1, mRec, DT_WORDBREAK)
    Printer.EndDoc
End Sub

This works fine provided I don't scale down the Printer's image so it can fit on a whole page. Once I do that, the text no longer is in the same relative position as the PictureBox version. I am looking for the scaling code that can achieve this.

Any ideas?
0
Comment
Question by:const71
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Rubyn
ID: 13742946
I checked Its working

Dim mrec  As RECT

    Let Picture1.ScaleMode = vbTwips

    Call Picture1.PaintPicture(LoadPicture("C:\Windows\Web\Wallpaper\Azul.jpg"), Picture1.ScaleLeft, Picture1.ScaleTop, Picture1.ScaleWidth, Picture1.ScaleHeight)
   
    mrec.Left = Picture1.ScaleX(1600, Picture1.ScaleMode, vbPixels)
    mrec.Right = mrec.Left + Picture1.ScaleX(3000, Picture1.ScaleMode, vbPixels)
    mrec.Top = Picture1.ScaleY(1000, Picture1.ScaleMode, vbPixels)
    mrec.Bottom = mrec.Top + Picture1.ScaleY(3000, Picture1.ScaleMode, vbPixels)
    Call DrawText(Picture1.hdc, mText, -1, mrec, DT_WORDBREAK)

    Let Printer.ScaleMode = vbTwips
   
    Call Printer.PaintPicture(LoadPicture("C:\Windows\Web\Wallpaper\Azul.jpg"), Printer.ScaleLeft, Printer.ScaleTop, Printer.ScaleWidth, Printer.ScaleHeight)
   
    mrec.Left = Printer.ScaleX(1600, Printer.ScaleMode, vbPixels)
    mrec.Right = mrec.Left + Printer.ScaleX(3000, Printer.ScaleMode, vbPixels)
    mrec.Top = Printer.ScaleY(1000, Printer.ScaleMode, vbPixels)
    mrec.Bottom = mrec.Top + Printer.ScaleY(3000, Printer.ScaleMode, vbPixels)
    Call DrawText(Printer.hdc, mText, -1, mrec, DT_WORDBREAK)
    Printer.EndDoc

Can you give the code which is not printing?
0
 

Author Comment

by:const71
ID: 13743596
I know this code is working!, but i want to scale down the Printer picture:

'==============================================================================
'      METHOD: PrintPictureToFitPage
' DESCRIPTION: This routine scales an image to fit the entire page as much as
'              possible. Useful when we want to print whole page invoices.
'==============================================================================
Public Sub PrintPictureToFitPage(pic As Picture)
    Dim PicRatio As Double
    Dim printerWidth As Double
    Dim printerHeight As Double
    Dim printerRatio As Double
    Dim printerPicWidth As Double
    Dim printerPicHeight As Double
       
    ' Determine if picture should be printed in landscape or portrait
    ' and set the orientation.
    If pic.Height >= pic.Width Then
        Printer.Orientation = vbPRORPortrait
    Else
        Printer.Orientation = vbPRORLandscape
    End If
   
    ' Calculate device independent Width-to-Height ratio for picture.
    PicRatio = pic.Width / pic.Height
    ' Calculate the dimensions of the printable area in HiMetric.
    printerWidth = Printer.ScaleX(Printer.ScaleWidth, Printer.ScaleMode, vbHimetric)
    printerHeight = Printer.ScaleY(Printer.ScaleHeight, Printer.ScaleMode, vbHimetric)
    ' Calculate device independent Width to Height ratio for printer.
    printerRatio = printerWidth / printerHeight
   
    ' Scale the output to the printable area.
    If (PicRatio >= printerRatio) Then
        ' Scale picture to fit full width of printable area.
        printerPicWidth = Printer.ScaleX(printerWidth, vbHimetric, Printer.ScaleMode)
        printerPicHeight = Printer.ScaleY(printerWidth / PicRatio, vbHimetric, Printer.ScaleMode)
    Else
        ' Scale picture to fit full height of printable area.
        printerPicHeight = Printer.ScaleY(printerHeight, vbHimetric, Printer.ScaleMode)
        printerPicWidth = Printer.ScaleX(printerHeight * PicRatio, vbHimetric, Printer.ScaleMode)
    End If
         
    Call Printer.PaintPicture(pic, 0, 0, printerPicWidth, printerPicHeight)
End Sub

When I load the Printer picture this way the text in the Printer object and PictureBox are no longer synchronized..Basically, I want to know where the CurrentX and CurrentY for Printer should be so they still look the same
0
 
LVL 5

Accepted Solution

by:
Rubyn earned 1000 total points
ID: 13743751
Did you try my previous code?

The better way is maintain the scalemode of printer and picture as Twips.

Private Sub Form_Load()
Dim Pic As Picture
    Set Pic = LoadPicture("c:\windows\web\wallpaper\azul.jpg")
    Call SetOrientation(Pic)
   
    Call PaintPicturePro(Picture1, Pic)
    Call PaintPicturePro(Printer, Pic)
    Printer.EndDoc
End Sub

Public Sub SetOrientation(Pic As Picture)
    If Pic.Height >= Pic.Width Then
        Printer.Orientation = vbPRORPortrait
    Else
        Printer.Orientation = vbPRORLandscape
    End If
End Sub

Public Sub PaintPicturePro(Device As Object, Pic As Picture)
    Dim PicRatio As Double

    If Device.ScaleHeight <= Device.ScaleWidth Then
        PicRatio = Pic.Width / Pic.Height
        Call Device.PaintPicture(Pic, 0, 0, Device.ScaleHeight * PicRatio, Device.ScaleHeight)
    Else
        PicRatio = Pic.Height / Pic.Width
        Call Device.PaintPicture(Pic, 0, 0, Device.ScaleWidth, Device.ScaleWidth * PicRatio)
    End If
End Sub
0
 

Author Comment

by:const71
ID: 13745393
Maybe I am not making myself clear

When I find the X and Y coordinates in the Picture1 (to write text to this exact location) I want to be able to use these EXACT SAME X and Y coordinates on the Printer object so the text can be over layed in the exact same location.

I WILL have situations where Picture1 and Printer will be scaled differently (although with the same aspect ratio).  The Picture1 image will always be larger than the Printer's image however I want the relative location of the text to appear at the same place in both.

I want to know what formula will adjust the X and Y coordinates so that the text will be adjusted on the correct place on the printer object.

Does that make sense?
0
 

Author Comment

by:const71
ID: 13745437
here is the code: you see that the text is not in the same location on the printer object as it is on the Picture1. Without changing Picture1, what code do i put to resolve the location of the text so it looks EXACTLY like Picture1


Option Explicit

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Const DT_WORDBREAK = &H10
Private Const mText = "THIS IS A LONG SENTENCE THAT WILL BE BROKEN DOWN INTO SEVERAL LINES"
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private mRec As RECT

Private Sub Command1_Click()
    Set Picture1.Picture = LoadPicture(App.Path & "\PORTRAIT.BMP")
    mRec.Left = Picture1.ScaleX(1600, Picture1.ScaleMode, vbPixels)
    mRec.Right = mRec.Left + Picture1.ScaleX(3000, Picture1.ScaleMode, vbPixels)
    mRec.Top = Picture1.ScaleY(1000, Picture1.ScaleMode, vbPixels)
    mRec.Bottom = mRec.Top + Picture1.ScaleY(3000, Picture1.ScaleMode, vbPixels)
    Call DrawText(Picture1.hDC, mText, -1, mRec, DT_WORDBREAK)

    Call PrintPictureToFitPage(LoadPicture(App.Path & "\PORTRAIT.BMP"))
    mRec.Left = Printer.ScaleX(1600, Printer.ScaleMode, vbPixels)
    mRec.Right = mRec.Left + Printer.ScaleX(3000, Printer.ScaleMode, vbPixels)
    mRec.Top = Printer.ScaleY(1000, Printer.ScaleMode, vbPixels)
    mRec.Bottom = mRec.Top + Printer.ScaleY(3000, Printer.ScaleMode, vbPixels)
    Call DrawText(Printer.hDC, mText, -1, mRec, DT_WORDBREAK)
    Call Printer.EndDoc
End Sub

Private Sub Form_Load()
    Picture1.ScaleMode = Printer.ScaleMode
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Text1.Text = X & ", " & Y
End Sub

'==============================================================================
'      METHOD: PrintPictureToFitPage
' DESCRIPTION: This routine scales an image to fit the entire page as much as
'              possible. Useful when we want to print whole page invoices.
'==============================================================================
Public Sub PrintPictureToFitPage(pic As Picture)
    Dim PicRatio As Double
    Dim printerWidth As Double
    Dim printerHeight As Double
    Dim printerRatio As Double
    Dim printerPicWidth As Double
    Dim printerPicHeight As Double
       
    ' Determine if picture should be printed in landscape or portrait
    ' and set the orientation.
    If pic.Height >= pic.Width Then
        Printer.Orientation = vbPRORPortrait
    Else
        Printer.Orientation = vbPRORLandscape
    End If
   
    ' Calculate device independent Width-to-Height ratio for picture.
    PicRatio = pic.Width / pic.Height
    ' Calculate the dimensions of the printable area in HiMetric.
    printerWidth = Picture1.ScaleX(Printer.ScaleWidth, Printer.ScaleMode, vbHimetric)
    printerHeight = Picture1.ScaleY(Printer.ScaleHeight, Printer.ScaleMode, vbHimetric)
    ' Calculate device independent Width to Height ratio for printer.
    printerRatio = printerWidth / printerHeight
   
    ' Scale the output to the printable area.
    If (PicRatio >= printerRatio) Then
        ' Scale picture to fit full width of printable area.
        printerPicWidth = Printer.ScaleX(printerWidth, vbHimetric, Printer.ScaleMode)
        printerPicHeight = Printer.ScaleY(printerWidth / PicRatio, vbHimetric, Printer.ScaleMode)
    Else
        ' Scale picture to fit full height of printable area.
        printerPicHeight = Printer.ScaleY(printerHeight, vbHimetric, Printer.ScaleMode)
        printerPicWidth = Printer.ScaleX(printerHeight * PicRatio, vbHimetric, Printer.ScaleMode)
    End If
         
    Call Printer.PaintPicture(pic, 0, 0, printerPicWidth, printerPicHeight)
End Sub
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question