Link to home
Start Free TrialLog in
Avatar of const71
const71

asked on

Printer and PictureBox question

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?
Avatar of Rubyn
Rubyn

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?
Avatar of const71

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Rubyn
Rubyn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of const71

ASKER

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?
Avatar of const71

ASKER

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