Google Answers Logo
View Question
 
Q: Visual Basic -> DrawWidth and the Printer Object ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Visual Basic -> DrawWidth and the Printer Object
Category: Computers > Programming
Asked by: oldman-ga
List Price: $10.00
Posted: 06 Sep 2002 12:41 PDT
Expires: 06 Oct 2002 12:41 PDT
Question ID: 62354
Good Day!

The following is an issue that concerns the Printer.Line function in
Visual Basic.

The Printer object allows you to set the DrawWidth for each line you
are printing. However, depending on the printer's resolution, the
actual line width on paper will vary. For example, same width line
will look thinner on a laser printer because it has a high resolution
comparing to ink jet printers. I am looking for some kind of formula
that will allow me to print lines that always look the same no matter
what printer resolution you have.

I hope I made myself clear. Otherwise, don't hesitate to ask for a
clarification.

Thank you for your time,
Eugene
Answer  
Subject: Re: Visual Basic -> DrawWidth and the Printer Object
Answered By: joseleon-ga on 06 Sep 2002 13:23 PDT
Rated:5 out of 5 stars
 
Hello, oldman:
 Your problem is a common problem when printing on windows, printer
resolution often is much higher than screen resolution and a unit
conversion is needed to make things look right. Not only lines are
affected by this problem, but images and text painted directly on the
printer canvas.
 
First you need to check out the ScaleX and ScaleY methods of the
printer:

ScaleX, ScaleY Methods 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vb98/html/vbmthscalex.asp

"The ScaleX and ScaleY methods take a value (width or height), with
its unit of measure specified by fromscale, and convert it to the
corresponding value for the unit of measure specified by toscale."

Next I will point you to a sample code which illustrates how to use
this methods:

Print a Picture to Fit a Page!
http://www.andreavb.com/tip070001.html

Here is the code:

Option Explicit

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 ' Taller than wide.
    Else
        Printer.Orientation = vbPRORLandscape ' Wider than tall.
    End If
    ' Calculate device independent Width-to-Height ratio for picture.
    PicRatio = Pic.Width / Pic.Height
    ' Calculate the dimentions 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
    ' Print the picture using the PaintPicture method.
    Printer.PaintPicture Pic, 0, 0, printerPicWidth, printerPicHeight
End Sub 

See how it uses the ScaleX and ScaleY methods to convert the image
width and height to the correct width and height depending on the
printer resolution. If you would try to use the PaintPicture method
with the pic.width and pic.height values without conversion, you would
get different sizes on different printers, bigger ink jet ones and
smaller on laser ones.

I hope this is the information you were looking for, and don't
hesitate to request for any clarification, we are here to help you.

Search terms used

visual basic faq
://www.google.com/search?q=visual+basic+faq&hl=es&lr=&ie=UTF-8&oe=utf-8&start=10&sa=N

Best regards.

Request for Answer Clarification by oldman-ga on 06 Sep 2002 14:35 PDT
Dear joseleon:

Thank you for this answer. I guess it points to the right direction: a
unit conversion has to be made. However, being a not very bright
programmer, I would really like to see how it applies to lines and not
necessarily pictures. I was hoping to see some code that would say:
Printer.DrawWidth = ???MagicFormula???

I am quite sure it's in the code you provided, but I just don't see it
because of by knowledge level.

Again, thank you for your time,
Eugene

Clarification of Answer by joseleon-ga on 06 Sep 2002 23:28 PDT
Hello, oldman:
  Don't worry, if you don't understand my answer is my fault because
we must provide as clearer answers as possible.
  
Your line:
  
  Printer.DrawWidth = ???MagicFormula???
  
Is:
  
  Dim yourDrawWidth As Double
  yourDrawWidth = 5
  Printer.DrawWidth = Printer.ScaleX(yourDrawWidth, vbPixels,
Printer.ScaleMode)
  
Let's imaging you want to draw 5 pixels wide lines, but the
printer.scalemode is set to twips, you need to use the ScaleX method
to convert your pixels to twips. Is like you were using distance units
when printing, a centimeter is always a centimeter on any printer no
matter the resolution of the printer.
  
Your MagicFormula is performed inside the ScaleX and ScaleY methods,
which perform the calculations to convert a device dependent unit
(like pixels) to an device independent unit (like twips).
  
twips - a whatis definition
http://whatis.techtarget.com/definition/0,,sid9_gci213233,00.html

I hope this time the answer is more clear and sorry for any
inconvenience.

Search strategy

twips
://www.google.com/search?q=twips&sourceid=mozilla-search&start=0&start=0&ie=utf-8&oe=utf-8

Best regards.
oldman-ga rated this answer:5 out of 5 stars
Thank you! That is exactly what I was looking for! Great job! A+++++

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy